我正在为倒数计时器编码,该计时器将在上午11点重置evry早晨,而我呼叫的日期是动态的,表示日期将每天更改。 一些我如何正确编码它somwtimes显示正确倒计时但通常它显示NAN NAN当我在Windows系统上随机检查它。
以下是我的代码 -
<div class="countdown">
<span>DEAL TIME LEFT : </span>
<p id="demo"></p>
</div>
<script>
var currentDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
var day = currentDate.getDate();
var month = currentDate.getMonth() + 1;
var year = currentDate.getFullYear();
//alert(year);
// Set the date we're counting down to
var countDownDate = new Date(day+" "+month+" "+year+", 11:00:00").getTime();
// 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 an 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);
document.getElementById("demo").innerHTML = hours + " : "
+ minutes + " : " + seconds;
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
</script>
即使我已经检查过,警报(白天),警报(月份),警报(年份)所有这些都是正确的但仍然是“NAN,NAN”错误
答案 0 :(得分:1)
你有一个问题
var countDownDate = new Date(day+" "+month+" "+year+", 11:00:00").getTime();
Date
构造函数无法解析您的字符串,而.getTime()
会返回NaN
。
而不是使用字符串。您可以使用Date
构造函数的其他参数。
新日期(年,月[,日期[,小时[,分钟[,秒] [, 毫秒]]]]]);
var countDownDate = new Date(year, month, day,11).getTime();
<div class="countdown">
<span>DEAL TIME LEFT : </span>
<p id="demo"></p>
</div>
<script>
var currentDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
var day = currentDate.getDate();
var month = currentDate.getMonth() ;
var year = currentDate.getFullYear();
//alert(year);
// Set the date we're counting down to
var countDownDate = new Date(year, month, day,11).getTime();
// 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 an 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);
document.getElementById("demo").innerHTML = hours + " : "
+ minutes + " : " + seconds;
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
</script>
&#13;
答案 1 :(得分:0)
我想你想倒数到上午11点,如果是11点之后,明天上午11点。
以下内容创建的结束日期为今天的11:00,如果是明天的11:00之后。然后它格式化剩余的时间。然后有一个计时器每隔一秒调用一次,就在下一整秒之后,所以它不会漂移。
它只创建一个日期,并显示如何添加日期并将其设置为特定时间。
希望评论足够。
function showRemaining() {
var endDate = new Date();
// If after 1100, add a day
if (endDate.getHours() > 11) {
endDate.setDate(endDate.getDate() + 1);
}
// Set time to 11am;
endDate.setHours(11,0,0,0);
// Get seconds from now to end
var diff = Math.ceil((endDate - Date.now())/1000);
// Show time as h:mm:ss
return ( (diff/3.6e3|0) + ':' +
('0'+((diff%3.6e3)/60|0)).slice(-2) + ':' +
('0'+(diff%60)).slice(-2)
);
}
// Run every second just after next full second
(function timer() {
console.log(showRemaining());
var lag = 1020 - (Date.now()%1000)
setTimeout(timer, lag);
}());
&#13;