【发布时间】:2018-08-21 02:39:19
【问题描述】:
要记住的关键变量:
设置为模拟晚上 11:00。
var bt = "23:00";
这设置为模拟上午 8:00。
var wt = "08:00";
所需的功能:
- 每天早上 8:00 开始倒计时。
- 每晚倒计时到晚上 11:00。
- 然后它停留在 00:00:00。
- 早上 8:00,它再次重复倒计时。
- 这应该永远发生。
实际情况:
- 一切正常,除了在午夜开始 24 小时倒计时,一直到上午 8:00。
我试过调试这个,我怀疑错误出在distance变量的计算上,使代码认为它正在与第二天进行比较,但我不知道如何解决这个问题。
这里是Codepen。
这是我的 JS 代码:
$(document).ready(function () {
var bt = "23:00"; // 11:00 PM
var wt = "08:00"; // 08:00 AM
var dateNow = moment().format('MMM D, YYYY');
placeHolderDate = dateNow + " " + bt;
var timeNow = moment().format('HH:mm');
var countDownDate = new Date(placeHolderDate).getTime();
var countDownHourMin = (wt.split(":"));
// 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 and 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);
$("#countDown").val(hours + ":" + minutes + ":" + seconds);
// If the countdown is over, write some text
if (hours === 0 && minutes === 0 && seconds === 0) {
//clearInterval(x);
$("#countDown").val("00:00:00");
}
if (hours < 0 || minutes < 0 || seconds < 0) {
//clearInterval(x);
$("#countDown").val("00:00:00");
}
var timeNow = moment().format('HH:mm');
//console.log('Time Now:' + timeNow);
//console.log('Wake Time:' + wt);
if (timeNow === wt) {
clearInterval(x);
restartCountdown();
}
//console.log(hours + ":" + minutes + ":" + seconds);
}, 1000);
function restartCountdown() {
//log("restartCountdown Started!");
var bt = "23:00"; // 11:00 PM
var wt = "08:00"; // 08:00 AM
var dN = (moment().add(moment.duration({d: 1})).format('MMM D, YYYY'));
console.log('dn ' + dN);
var placeHolderDate = dN + " " + bt;
var countDownDate = new Date(placeHolderDate).getTime();
var countDownHourMin = (wt.split(":"));
// 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 and 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);
$("#countDown").val(hours + ":" + minutes + ":" + seconds);
// If the countdown is over, write some text
if (hours === 0 && minutes === 0 && seconds === 0) {
//clearInterval(x);
$("#countDown").val("00:00:00");
}
if (hours < 0 || minutes < 0 || seconds < 0) {
//clearInterval(x);
$("#countDown").val("00:00:00");
}
// console.log(hours + ":" + minutes + ":" + seconds);
}, 1000);
}
});
【问题讨论】:
-
如果我没记错的话,您希望它在
23:00 - 08:00之间停止。哪些线路可以完成这项工作?这与您的if (timeNow === wt)矛盾,restartCountdown会立即重新启动它。 -
@choz 正确。我希望它在 23:00 - 08:00 之间停止 - 或者至少在视觉上向用户显示 00:00:00。
if语句实际上是在上午 08:00 正确启动它。另外,请注意wt和bt在实际应用中是可以更改的,这里为了演示而进行了硬编码。
标签: javascript time momentjs countdown