简体   繁体   English

使倒数计时器 go 到上午 9:30,然后是上午 11 点,然后重置到下周日

[英]Make countdown timer go till 9:30am, then 11am and then reset to next Sunday

I have the following code, but want to adjust so that we can have the countdown for two different times.我有以下代码,但想调整以便我们可以倒计时两个不同的时间。

 var secTime; var ticker; function getSeconds() { var nowDate = new Date(); var dy = 1; //Sunday through Saturday, 0 to 6 var countertime = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate(), 21, 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; }
 <body onload="getSeconds();"> <h6>Live in <span class="days" id="days"></span><span class="smalltext"> days,</span> <span class="hours" id="hours"></span><span class="smalltext"> hours,</span> <span class="minutes" id="minutes"></span><span class="smalltext"> minutes</span> <span class="seconds" id="seconds"></span><span class="smalltext"> seconds</span> </h6> </body>

But I want to have the countdown go to 9:30am and then as soon as it hits 9:30am, it starts counting down till 11am.但是我想让 go 倒计时到上午 9:30,然后一旦到达上午 9:30,它就会开始倒计时到上午 11 点。 After that reset and go to the next Sunday.之后重置和 go 到下周日。 How would I accomplish this?我将如何做到这一点?

for simplicity we'll assume it is turned on a weekday.为简单起见,我们假设它在工作日打开。 so we just need to count to target date1, then target date2.所以我们只需要计数到目标日期1,然后目标日期2。

 var secTime; var ticker; var mode = "weekday"; // or "sunday" depending on a function yet to be written function getSeconds() { var nowDate = new Date(); var dy = 1; //Sunday through Saturday, 0 to 6 if (mode == "weekday") { var countertime = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate(), 09, 30, 00); } if (mode == "sunday") { var countertime = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate(), 11, 00, 00); } if (mode == "sunday") { mode = "weekday" } else { mode = "sunday" } 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; }
 <body onload="getSeconds();"> <h6>Live in <span class="days" id="days"></span><span class="smalltext"> days,</span> <span class="hours" id="hours"></span><span class="smalltext"> hours,</span> <span class="minutes" id="minutes"></span><span class="smalltext"> minutes</span> <span class="seconds" id="seconds"></span><span class="smalltext"> seconds</span> </h6> </body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM