Tengo el siguiente código, pero quiero ajustarlo para que podamos tener la cuenta regresiva para dos momentos diferentes.
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>
Pero quiero que la cuenta regresiva llegue a las 9:30 a. m. y luego, tan pronto como llegue a las 9:30 a. m., comience la cuenta regresiva hasta las 11 a. m. Después de eso reinicia y pasa al siguiente domingo. ¿Cómo lograría esto?
para simplificar, supondremos que está encendido un día de la semana. entonces solo necesitamos contar hasta la fecha objetivo1, luego la fecha objetivo2.
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>