lundi 9 septembre 2019

Having trouble setting up a multiple case countdown timer in javascript

I I'm setting up a countdown timer that will count down to two different times a day over the course of a week, so a total of 14 different cases. I've tried a switch statement, and an if-else was recommended to me instead. However, I cannot seem to get my code to work, if anybody could help it would be greatly appreciated.

var curday;
var secTime;
var ticker;


function getSeconds() {

 var nowDate = new Date(); 

 if (nowDate.getDay() = 0 && nowDate.getHours() < 17) {
     var dy = 0; //Sunday 1
     var countertime = new Date(nowDate.getFullYear(),nowDate.getMonth(),nowDate.getDate(),17,0,0); //20 out of 24 hours = 8pm
 } else if (nowDate.getDay() = 0 && nowDate.getHours() < 23 && nowDate.getMinutes() < 15) {
     var dy = 0; //Sunday 2
     var countertime = new Date(nowDate.getFullYear(),nowDate.getMonth(),nowDate.getDate(),17,0,0); //20 out of 24 hours = 8pm
 } else if (nowDate.getDay() = 1 && nowDate.getHours() < 10) {
     var dy = 1; //Monday 1
     var countertime = new Date(nowDate.getFullYear(),nowDate.getMonth(),nowDate.getDate(),10,0,0); //20 out of 24 hours = 8pm
 } else if (nowDate.getDay() = 1 && nowDate.getHours() < 23 && nowDate.getMinutes() < 15) {
     var dy = 1; //Monday 2
     var countertime = new Date(nowDate.getFullYear(),nowDate.getMonth(),nowDate.getDate(),17,0,0); //20 out of 24 hours = 8pm
 }

 var curtime = nowDate.getTime(); //current time
 var atime = countertime.getTime(); //countdown time

 var diff = parseInt((atime - curtime)/1000);
 if (diff > 0) { curday = dy - nowDate.getDay() }
 else { curday = dy - nowDate.getDay() -1 } //after countdown time
 if (curday < 0) { curday += 7; } //already after countdown time, switch to next week
 if (diff <= 0) { diff += (86400 * 7) }
 startTimer (diff);

}

function startTimer(secs) {
 secTime = parseInt(secs);
 ticker = setInterval("tick()",1000);
 tick(); //initial count display
}

function tick() {
 var secs = secTime;
 if (secs>0) {
  secTime--;
 }
 else {
  clearInterval(ticker);
  getSeconds(); //start over
 }

 var days = Math.floor(secs/86400);
 secs %= 86400;
 var hours= Math.floor(secs/3600);
 secs %= 3600;
 var mins = Math.floor(secs/60);
 secs %= 60;

 //update the time display
 document.getElementById("days").innerHTML = curday;
 document.getElementById("hours").innerHTML = ((hours < 10 ) ? "0" : "" ) + hours;
 document.getElementById("minutes").innerHTML = ( (mins < 10) ? "0" : "" ) + mins;
 document.getElementById("seconds").innerHTML = ( (secs < 10) ? "0" : "" ) + secs;
}

I expect it to produce the values used to update the display, however it does nothing.

Aucun commentaire:

Enregistrer un commentaire