Condition dans un script

symbol

Membre actif
13 Octobre 2004
536
25
Bonjour

J'ai un script (en 2 parties) qui affiche un compte a rebours.

Il y'a 1 condition pour l'execution du script :
- Si l'heure est entre 14h16 et 21h15 la 1ere partie du script s'execute, sinon, si l'heure est entre 21h16 et 14h15 la partie N° 2 s'execute.


J'ai vu qu'il yavait ca :
Bloc de code:
if (condition) {
       Instruction
    }
    else {
       Instruction
    }

Quelles instructions faut-il mettre a la place de "Instruction" pour que mes conditions soient executées ?

Merci



Voici la partie du script qui affiche le compte a rebours en question a 14h15 :
Bloc de code:
<!-- AFFICHAGE COMPTE A REBOURS pour RECAP  -->
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
p {
  text-align: center;
  font-size: 12px;
  margin-top:0px;
}
</style>
</head>
<body>

PROCHAIN RECAP

<p id="demo"></p>

<script>
// Set the date we're counting down to
var countDownDate = new Date("Sep 5, 2018 14:15:00").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

    // Get todays date and time
    var now = new Date().getTime();
  
    // Find the distance between now an the count down date
    var distance = countDownDate - now;
  
    // Time calculations for days, hours, minutes and seconds
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);
  
    // Output the result in an element with id="demo"
    document.getElementById("demo").innerHTML =  hours + "h "
    + minutes + "m " + seconds + "s ";
  
    // If the count down is over, write some text
    if (distance < 0) {
        clearInterval(x);
        document.getElementById("demo").innerHTML = "RECAP de 14h15 effectué";
    }
}, 1000);
</script>

</body>
</html>
 
J'ai essayé ca :
Bloc de code:
<script>

    // Get todays date and time
    var now = new Date().getTime();

if now >14h15 <21h15 {
// Set the date we're counting down to
var countDownDate = new Date("Sep 5, 2018 14:15:00").getTime();
    }
    else {
// Set the date we're counting down to
var countDownDate = new Date("Sep 5, 2018 21:15:00").getTime();
    }

// Update the count down every 1 second
var x = setInterval(function() {

    // Get todays date and time
    var now = new Date().getTime();
   
    // Find the distance between now an the count down date
    var distance = countDownDate - now;
   
    // Time calculations for days, hours, minutes and seconds
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);
   
    // Output the result in an element with id="demo"
    document.getElementById("RECAP").innerHTML =  hours + "h "
    + minutes + "m " + seconds + "s ";
   
    // If the count down is over, write some text
    if (distance < 0) {
        clearInterval(x);
        document.getElementById("RECAP").innerHTML = "RECAP de 14h15 effectué";
    }
}, 1000);
</script>

Mais ca marche pas . C'est pas etonant non plus, vu que je connais pas la bonne syntaxe pour ca (j'ai mis ce qui me parait logique) -> if now >14h15 <21h15 {
 
Les fonctions javascript sur les dates : https://www.w3schools.com/jsref/jsref_obj_date.asp

A vu de nez :
Bloc de code:
//avoir l'heure en minutes (tu peux aussi faire en secondes)
var now = new Date();
var minute = now.getHours()*60 + now.getMinutes();

//14h16 = 856
//21h15 = 1275
if (minute > 856 && minute < 1275)
    Entre 14h16 et 21h15
} else {
    Avant ou apres
}
 
Merci de tes indications .

J'ai fait :
Bloc de code:
<!-- AFFICHAGE COMPTE A REBOURS pour RECAP  -->
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
p {
  text-align: center;
  font-size: 12px;
  margin-top:0px;
}
</style>
</head>
<body>

PROCHAIN RECAP

<p id="RECAP"></p>

<script>
//avoir l'heure en minutes (tu peux aussi faire en secondes)
var now = new Date();
var minute = now.getHours()*60 + now.getMinutes();

//14h16 = 856
//21h15 = 1275
if (minute > 856 && minute < 1275)

// Update the count down every 1 second
var x = setInterval(function() {

    // Get todays date and time
    var now = new Date().getTime();
 
    // Find the distance between now an the count down date
    var distance = countDownDate - now;
 
    // Time calculations for days, hours, minutes and seconds
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);
 
    // Output the result in an element with id="RECAP"
    document.getElementById("RECAP").innerHTML =  hours + "h "
    + minutes + "m " + seconds + "s ";
 
    // If the count down is over, write some text
    if (distance < 0) {
        clearInterval(x);
        document.getElementById("RECAP").innerHTML = "RECAP de 21h15 effectué";
    }
}, 1000);

} else {
   // Update the count down every 1 second
var x = setInterval(function() {

    // Get todays date and time
    var now = new Date().getTime();
 
    // Find the distance between now an the count down date
    var distance = countDownDate - now;
 
    // Time calculations for days, hours, minutes and seconds
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);
 
    // Output the result in an element with id="demo"
    document.getElementById("RECAP").innerHTML =  hours + "h "
    + minutes + "m " + seconds + "s ";
 
    // If the count down is over, write some text
    if (distance < 0) {
        clearInterval(x);
        document.getElementById("RECAP").innerHTML = "RECAP de 14h15 effectué";
    }
}, 1000);
}
</script>

</body>
</html>

Ca affiche rien, sauf "PROCHAIN RECAP"
 
J'ai modifié ton code pour que ce soit plus lisible. Ca fonctionne correctement après avoir rajouté le "{ " manquant.
Après, je ne suis pas certain que tes fonctions marchent correctement, en tout cas ça les lance.

Bloc de code:
<!-- AFFICHAGE COMPTE A REBOURS pour RECAP  -->
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
p {
  text-align: center;
  font-size: 12px;
  margin-top:0px;
}
</style>
</head>
<body>

PROCHAIN RECAP

<p id="RECAP"></p>

<script>

function fonction_1() {
  document.getElementById("RECAP").innerHTML = "fonction 1";

  // Get todays date and time
  var now = new Date().getTime();

  // Find the distance between now an the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Output the result in an element with id="RECAP"
  document.getElementById("RECAP").innerHTML =  hours + "h " + minutes + "m " + seconds + "s ";

  // If the count down is over, write some text
  if (distance < 0) {
      clearInterval(x);
      document.getElementById("RECAP").innerHTML = "RECAP de 21h15 effectué";
  }
}

function fonction_2() {
  document.getElementById("RECAP").innerHTML = "fonction 2";

  // Get todays date and time
  var now = new Date().getTime();

  // Find the distance between now an the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Output the result in an element with id="demo"
  document.getElementById("RECAP").innerHTML =  hours + "h " + minutes + "m " + seconds + "s ";

  // If the count down is over, write some text
  if (distance < 0) {
      clearInterval(x);
      document.getElementById("RECAP").innerHTML = "RECAP de 14h15 effectué";
  }
}

//avoir l'heure en minutes (tu peux aussi faire en secondes)
var now = new Date();
var minute = now.getHours()*60 + now.getMinutes();

//14h16 = 856
//21h15 = 1275
if (minute > 856 && minute < 1275) {
  // Update the count down every 1 second
  setInterval(fonction_1, 1000);
} else {
  // Update the count down every 1 second
  setInterval(fonction_2, 1000);
}

</script>

</body>
</html>
 
Trouvé, ton countDownDate avait disparu ;)
Pour le "fonction 1" qui s'affichait, c'était un test :D

Bloc de code:
<!-- AFFICHAGE COMPTE A REBOURS pour RECAP  -->
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
p {
  text-align: center;
  font-size: 12px;
  margin-top:0px;
}
</style>
</head>
<body>

PROCHAIN RECAP

<p id="RECAP"></p>

<script>

function fonction_1() {
  // Get todays date and time
  var now = new Date().getTime();

  // Find the distance between now an the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Output the result in an element with id="RECAP"
  document.getElementById("RECAP").innerHTML =  hours + "h " + minutes + "m " + seconds + "s ";

  // If the count down is over, write some text
  if (distance < 0) {
      clearInterval(x);
      document.getElementById("RECAP").innerHTML = "RECAP de 21h15 effectué";
  }
}

function fonction_2() {
  // Get todays date and time
  var now = new Date().getTime();

  // Find the distance between now an the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Output the result in an element with id="demo"
  document.getElementById("RECAP").innerHTML =  hours + "h " + minutes + "m " + seconds + "s ";

  // If the count down is over, write some text
  if (distance < 0) {
      clearInterval(x);
      document.getElementById("RECAP").innerHTML = "RECAP de 14h15 effectué";
  }
}

//avoir l'heure en minutes (tu peux aussi faire en secondes)
var now = new Date();
var minute = now.getHours()*60 + now.getMinutes();

//14h16 = 856
//21h15 = 1275
var countDownDate = new Date("Sep 5, 2018 21:15:00").getTime();
if (minute > 856 && minute < 1275) {
  // Update the count down every 1 second
  setInterval(fonction_1, 1000);
} else {
  // Update the count down every 1 second
  setInterval(fonction_2, 1000);
}

</script>

</body>
</html>
 
bonjour

Autant hier ca fonctionnait bien, que ce matin, le script indique le prochain evenement (recap) dans 09h5901s (alors qu'il est 11h16, et l'evenement doit se produire a 14h15).

Je recapitule :
Un évenement (lancement d'un script sur ma machine, appelé RECAP) doit se produire a 14h15 et 21h15.
Le compte a rebours, doit indiquer (sur mon forum) combien de temps il reste avant le lancement de chaque évenement.

Il me semble que le probleme vient du fait qu'une seule heure est indiquée dans le script
"21:15:00". Il y a une autre heure ou l'evenement doit se produire -> 14:15:00

Du coup, j'ai changé l'heure qui est indiqué dans le script (21:15:00) par 14:15:00
Maintenant le compte a rebours fonctionne.
Mais pour ce soir ? il faut qu'il fasse un decompte a partir de 14:16 jusqu'a 21h15 .


Principe du fonctionnement
--------------------------------------
- HEURE ACTUELLE --> compte a rebours (en H:M:S) jusqu'a 14h15
- Une fois l'evenement à 14h15 réalisé --> compte a rebours de 14h16 jusqu'a 21h15 (en H:M:S)
- une fois l'evenement de 21h15 réalisé --> compte a rebours jusqu'a 14h15
....et ainsi de suite....
 

Fichiers joints

  • Capture d’écran 2018-06-25 à 11.15.07.png
    Capture d’écran 2018-06-25 à 11.15.07.png
    16,2 KB · Affichages: 247
Dernière édition:
Version améliorée et fonctionnelle :)

Bloc de code:
<!-- AFFICHAGE COMPTE A REBOURS pour RECAP  -->
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
p {
  text-align: center;
  font-size: 12px;
  margin-top:0px;
}
</style>
</head>
<body>

PROCHAIN RECAP

<p id="RECAP"></p>

<script>

function fonction_1() {
  // Get todays date and time
  var now = new Date();
  var secondes = now.getHours()*3600 + now.getMinutes()*60 + now.getSeconds();

  // Find the distance between now an the count down date
  var distance = 51300 - secondes;

  // Time calculations for days, hours, minutes and seconds
  var seconds = distance % 60;
  var minutes = (distance - seconds) / 60 % 60;
  var hours = (distance - seconds - minutes*60) / 3600;

  // Output the result in an element with id="RECAP"
  document.getElementById("RECAP").innerHTML = hours + "h " + minutes + "m " + seconds + "s ";

  // If the count down is over, write some text
  if (distance < 0) {
    clearInterval();
    document.getElementById("RECAP").innerHTML = "RECAP de 14h15 effectué";
  }
}

function fonction_2() {
  // Get todays date and time
  var now = new Date();
  var secondes = now.getHours()*3600 + now.getMinutes()*60 + now.getSeconds();

  // Find the distance between now an the count down date
  var distance = 76500 - secondes;

  // Time calculations for days, hours, minutes and seconds
  var seconds = distance % 60;
  var minutes = (distance - seconds) / 60 % 60;
  var hours = (distance - seconds - minutes*60) / 3600;

  // Output the result in an element with id="RECAP"
  document.getElementById("RECAP").innerHTML = hours + "h " + minutes + "m " + seconds + "s ";

  // If the count down is over, write some text
  if (distance < 0) {
    clearInterval();
    document.getElementById("RECAP").innerHTML = "RECAP de 21h15 effectué";
  }
}

function fonction_3() {
  // Get todays date and time
  var now = new Date();
  var secondes = now.getHours()*3600 + now.getMinutes()*60 + now.getSeconds();

  // Find the distance between now an the count down date
  if ( secondes > 76500 ) {
    var distance = 137700 - secondes;
  } else {
    var distance = 51300 - secondes;
  }

  // Time calculations for days, hours, minutes and seconds
  var seconds = distance % 60;
  var minutes = (distance - seconds) / 60 % 60;
  var hours = (distance - seconds - minutes*60) / 3600;

  // Output the result in an element with id="RECAP"
  document.getElementById("RECAP").innerHTML = hours + "h " + minutes + "m " + seconds + "s ";

  // If the count down is over, write some text
  if (distance < 0) {
    clearInterval();
    document.getElementById("RECAP").innerHTML = "RECAP de 14h15 effectué";
  }
}

// Avoir le nombre de secondes depuis 0h00 pour l'heure actuelle
var date = new Date();
var now = date.getHours()*3600 + date.getMinutes()*60 + date.getSeconds();

/*
Tes deux heures en secondes :
14h15 = 50400 + 900 = 51300 (le lendemain : 14h15 + 24h = 137700)
21h15 = 75600 + 900 = 76500
*/
if ( now < 51300 ) {
  // avant 14h15
  setInterval(fonction_1, 1000);
} else {
  if ( now < 76500 ) {
    // entre 14h15 et 21h15
    setInterval(fonction_2, 1000);
  } else {
    // après 21h15
    setInterval(fonction_3, 1000);
  }
}

</script>

</body>
</html>
 
  • J’aime
Réactions: pouppinou
Merci :)

J'essai de suite.

Pour le moment ca fonctionne

Capture_d_e_cran_2018_06_26_a_09_55_34.png
 
Pour ce soir, ca semble ok aussi :)
 

Fichiers joints

  • Capture d’écran 2018-06-26 à 15.20.56.png
    Capture d’écran 2018-06-26 à 15.20.56.png
    16,1 KB · Affichages: 230