【发布时间】:2017-02-13 09:40:49
【问题描述】:
我正在编写一个倒数计时器,它会在每天早上 11 点重置,我打电话的日期是动态的,意味着日期每天都会改变。 有些我是如何正确编码的,它有时会显示正确的倒计时,但当我在 Windows 系统上随机检查它时,它通常会显示 NAN NAN。
下面是我的代码-
<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>
即使我检查了,alert(day),alert(month), alert(year)所有这些都是正确的,但我仍然收到 “NAN,NAN”错误.
【问题讨论】:
标签: javascript html magento-1.9