« Programmation JavaScript/Programmer en deux minutes/Compteur en temps réel » : différence entre les versions

Contenu supprimé Contenu ajouté
DannyS712 (discussion | contributions)
m <source> -> <syntaxhighlight> (phab:T237267)
Révision de l'exemple : formatage + traduction + sans la mauvaise pratique du retour par variable globale
Ligne 29 :
:Si vous êtes un utilisateur enregistré de Wikipédia, vous pouvez personnaliser votre JavaScript pour ajouter à votre interface plein de choses, dont bien sûr ce compteur. (voir [[w:Discussion utilisateur:Al Maghi/Gadget-WarCost.js|Gadget-WarCost.js]])
 
<syntaxhighlight lang="javascript">
//////////////////////Start CUSTOMIZATION//////////////////////
//// Le budget affiché :
//var Gadget_WarCost_WhichWar="US in Afghanistan since 2001";
//var Gadget_WarCost_WhichWar="US in Iraq since 2003";
//var Gadget_WarCost_WhichWar="US in Wars since 2001";
var Gadget_WarCost_WhichWar="French military budget since 2009";
////Datas:
var totalIraq = 687000000000; // total dollars allocated to Iraq War.
var startOfIraqWar = new Date("Mar 20, 2003"); // start of Iraq War.
var IraqBudgetedThrough = new Date("Sept 30, 2009"); // current budget goes to
var totalAg = 228000000000; // total dollars allocated to Afghanistan War.
var startOfAgWar = new Date("Oct 7, 2001"); // start of Afghanistan War.
var AgBudgetedThrough = new Date("Sept 30, 2009"); // current budget goes to
var totalFM=186000000000; //budget militaire français 2009 - 2014
var startOfFM=new Date("Jan 1, 2009");
var FMBudgetedThrough=new Date("Dec 31, 2014");
 
//// Données :
var separator = "'";
var totalIraq = 687000000000; // total dollars allocated to Iraq War.
var startOfIraqWar = new Date("Mar 20, 2003"); // start of Iraq War.
/////////////////End CUSTOMIZATION/////////////////
var IraqBudgetedThrough = new Date("Sept 30, 2009"); // current budget goes to
 
RunWarCost(); ////// Exécute la fonction principale. //
var totalAg = 228000000000; // total dollars allocated to Afghanistan War.
var startOfAgWar = new Date("Oct 7, 2001"); // start of Afghanistan War.
function RunWarCost(){
var AgBudgetedThrough = new Date("Sept 30, 2009"); // current budget goes to
if (Gadget_WarCost_WhichWar == "US in Wars since 2001") { //regarde quelles données afficher.
 
calculateAg();
var totalFM=186000000000; //budget militaire français 2009 - 2014
calculateIraq();
var startOfFM=new Date("Jan 1, 2009");
calculateTotal();
var FMBudgetedThrough=new Date("Dec 31, 2014");
var OutNumberWarCost=costOfTotal;
 
}
var separator = "'";
else if (Gadget_WarCost_WhichWar == "US in Iraq since 2003") {
 
calculateIraq();
/////////////////End CUSTOMIZATION/////////////////
var OutNumberWarCost=costOfIraq;
 
}
RunWarCost(); ////// Exécute la fonction principale. //
else if (Gadget_WarCost_WhichWar =="US in Afghanistan since 2001") {
 
calculateAg();
function RunWarCost()
var OutNumberWarCost=costOfAg;
{
}
var WarCostmoney = "$";
else if (Gadget_WarCost_WhichWar =="French military budget since 2009") {
var OutNumberWarCost = 0;
calculateFrenchMilitary();
 
WarCostmoney =" euros";
// Regarde quelles données afficher.
var OutNumberWarCost=costOfFrenchMilitary;
if (Gadget_WarCost_WhichWar == "US in Wars since 2001")
}
{
else {alert("The value of parameter Gadget_WarCost_WhichWar is wrong.");}
OutNumberWarCost = calculateTotal();
}
if (WarCostmoney=="$") { var OutTextCostOfWar=WarCostmoney+number_str(OutNumberWarCost);}
else if (Gadget_WarCost_WhichWar == "US in Iraq since 2003")
else { var OutTextCostOfWar=number_str(OutNumberWarCost)+WarCostmoney; } //La chaine de caractere du chiffre actuel
{
OutNumberWarCost = calculateIraq();
var compteur = document.getElementById("compteur");
}
compteur.innerHTML= "Votre compteur : " + OutTextCostOfWar + " = " + Gadget_WarCost_WhichWar; //Affiche la chaine de caractere
else if (Gadget_WarCost_WhichWar =="US in Afghanistan since 2001")
{
window.setTimeout(RunWarCost, 100); ////// Réexécute la fonction principale dans 100 millisecondes. //
OutNumberWarCost = calculateAg();
}
}
else if (Gadget_WarCost_WhichWar =="French military budget since 2009")
function calculateFrenchMilitary () {
{
calculateWarCost(startOfFM,FMBudgetedThrough,totalFM);
OutNumberWarCost = calculateFrenchMilitary();
costOfFrenchMilitary=costOfWarAmount;
WarCostmoney = " €";
}
}
function calculateIraq () {
else alert("The value of parameter Gadget_WarCost_WhichWar is wrong.");
calculateWarCost(startOfIraqWar,IraqBudgetedThrough,totalIraq);
 
costOfIraq=costOfWarAmount;
var OutTextCostOfWar = number_str(OutNumberWarCost); // Formatage du nombre
}
if (WarCostmoney=="$")
function calculateAg () {
OutTextCostOfWar = WarCostmoney + OutTextCostOfWar; // Symbole monétaire avant
calculateWarCost(startOfAgWar,AgBudgetedThrough,totalAg);
else
costOfAg=costOfWarAmount;
OutTextCostOfWar = OutTextCostOfWar + WarCostmoney; // Symbole monétaire après
}
 
function calculateTotal () {
var compteur = document.getElementById("compteur");
costOfTotal = costOfAg + costOfIraq;
compteur.innerHTML= "Votre compteur : " + OutTextCostOfWar + " = " + Gadget_WarCost_WhichWar; // Affiche la chaîne de caractère
}
 
window.setTimeout(RunWarCost, 1000); ////// Ré-exécute la fonction principale dans 1 seconde.
function calculateWarCost (startOfWar,BudgetedThrough,totalMoney) {
}
var totalMS = BudgetedThrough - startOfWar; // total MS for dollars allocated
 
var ratePerMS = totalMoney / totalMS; // the rate per MS of the war so far
function calculateFrenchMilitary()
var curDate = new Date(); // today's date
{
var diff = curDate - startOfWar; // MS between today and start of the war
calculateWarCost(startOfFM, FMBudgetedThrough, totalFM);
costOfWarAmount = diff * ratePerMS; // cost of war at this time
costOfFrenchMilitary=costOfWarAmount;
}
}
function number_strcalculateIraq(n){
{
var x=n.toString();
return calculateWarCost(startOfIraqWar, IraqBudgetedThrough, totalIraq);
var dot=x.lastIndexOf('.');
costOfIraq=costOfWarAmount;
x=x.substr(0,dot);
}
var l=x.length;
function calculateAg()
var res="";
{
for(l-=3;l>0;l-=3){res=separator+x.substr(l,3)+res;}
return calculateWarCost(startOfAgWar, AgBudgetedThrough, totalAg);
res=x.substr(0,l+3)+res;
costOfAg=costOfWarAmount;
return res;
}
function calculateTotal()
{
return calculateAg() + calculateIraq();
}
 
function calculateWarCost(startOfWar, BudgetedThrough, totalMoney)
{
var totalMS = BudgetedThrough - startOfWar; // Durée du budget en millisecondes
var ratePerMS = totalMoney / totalMS; // Budget par milliseconde
var curDate = new Date(); // Date courante
var diff = curDate - startOfWar; // Millisecondes depuis le début de la guerre
return diff * ratePerMS; // Coût de la guerre jusqu'à maintenant
}
 
function number_str(n)
{
var x = n.toString();
var dot = x.lastIndexOf('.');
x = x.substr(0,dot);
var l = x.length;
var res = "";
for(l-=3 ; l>0 ; l-=3)
res = separator+x.substr(l,3) + res;
res = x.substr(0,l+3) + res;
return res;
}
</syntaxhighlight>