Javascript 如何防止每日倒计时计时器在午夜重置?
要记住的关键变量: 这设置为模拟晚上11:00。 var bt=23:00; 这设置为模拟上午8:00。 var wt=08:00 所需的功能: 倒数计时器每天早上8:00开始。 每天晚上倒数到晚上11点。 然后它停留在00:00:00。 早上8点,它再次重复倒计时。 这应该永远发生。 实际发生的情况: 一切正常,除了在午夜开始24小时倒计时,直到早上8点。 我已经试过调试这个,我怀疑错误在于计算的距离变量,使代码认为它与第二天进行了比较,但我不确定如何纠正这个错误 这是你的电话号码 这是我的JS代码:Javascript 如何防止每日倒计时计时器在午夜重置?,javascript,time,momentjs,countdown,Javascript,Time,Momentjs,Countdown,要记住的关键变量: 这设置为模拟晚上11:00。 var bt=23:00; 这设置为模拟上午8:00。 var wt=08:00 所需的功能: 倒数计时器每天早上8:00开始。 每天晚上倒数到晚上11点。 然后它停留在00:00:00。 早上8点,它再次重复倒计时。 这应该永远发生。 实际发生的情况: 一切正常,除了在午夜开始24小时倒计时,直到早上8点。 我已经试过调试这个,我怀疑错误在于计算的距离变量,使代码认为它与第二天进行了比较,但我不确定如何纠正这个错误 这是你的电话号码 这是我的J
$(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);
}
});
我已编辑了您的代码并创建了此代码笔。请随意优化代码,因为我还没有这样做。这个想法是检查时间是否在上午8点到晚上11点之间。如果是,则显示值,否则显示00:00:00。同样,一旦日期发生变化,更新日期,现在进行相应的计算 $document.readyfunction{
function countdown() {
var bt = "23:00", // 11:00 PM
wt = "08:00"; // 08:00 AM
var today = new Date(),
dd = today.getDate(),
mm = today.getMonth()+1,
yyyy = today.getFullYear();
var startTime = new Date(mm + '/' + dd + '/' + yyyy + ' ' + wt),
endTime = new Date(mm + '/' + dd + '/' + yyyy + ' ' + bt);
setInterval(function() {
var now = new Date();
var nowdd = today.getDate();
var nowTime = now.getTime();
if(dd !== nowdd) {
dd = nowdd;
var nowmm = now.getMonth() + 1,
nowyyyy = now.getFullYear();
startTime = new Date(dd + '/' + mm + '/' + yyyy + ' wt');
endTime = new Date(dd + '/' + mm + '/' + yyyy + ' bt');
}
if(nowTime > startTime && nowTime < endTime) {
// Find the distance between now and the count down date
var distance = endTime - nowTime;
// Time calculations for days, hours, minutes and seconds
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)),
minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)),
seconds = Math.floor((distance % (1000 * 60)) / 1000);
$("#countDown").val(hours + ":" + minutes + ":" + seconds);
} else {
$("#countDown").val("00:00:00");
}
}, 1000);
}
countdown();
});
如果我没有弄错的话,您希望它在23:00-08:00之间停止。哪些行可以执行该任务?这与您的If-timeNow===wt相矛盾,哪个restartCountdown会立即重新启动它。@choz Correct。我希望它在23:00-08:00之间停止-或者至少在视觉上向用户显示00:00:00。If语句实际上正在启动它正确的时间是上午8:00。另外,请注意,wt和bt可以在实际应用程序中更改,并在此处进行硬编码以供演示。这里似乎存在错误。根据逻辑和代码,尝试以下时间:bt=02:30和wt=09:00。wt必须是2:30和bt必须是9:00。我尝试了2:30和9:00,但结果相同t、 例如,现在的时间是我所在的晚上11:22。我将bt设置为凌晨2:00,wt设置为上午9:00,但计数器仍然显示00:00:00ok,这意味着第1天的上午9:00和第2天的上午2:00。在这种情况下,您需要在EndTime中添加一天。我编辑了代码笔,因此您没有提供结束时间,而是提及开始时间y后的小时数您希望计数器运行。这将在日期发生变化时考虑这一事实