繁体   English   中英

NaN关于Internet Explorer中的Javascript倒数计时器

[英]NaN on Javascript countdown timer in Internet Explorer

我将以下JavaScript用作倒数计时器,并且在大多数浏览器中都可以正常运行,但是我只是仔细检查了Internet Explorer,但是却显示了“ NaN”来代替每个数字。

任何人都可以帮助解释IE中的错误之处,而不是将单个变量视为数字吗?

 // Set the date we're counting down to var countDownDate = new Date("2018-05-25 12: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 days = Math.floor(distance / (1000 * 60 * 60 * 24)); 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); if (days.toString().length < 2) { days = "0" + days; } if (hours.toString().length < 2) { hours = "0" + hours; } if (minutes.toString().length < 2) { minutes = "0" + minutes; } if (seconds.toString().length < 2) { seconds = "0" + seconds; } // Display the result in the element with id="countdown" document.getElementById("countdown").innerHTML = days + " : " + hours + " : " + minutes + " : " + seconds; // If the count down is finished, write some text if (distance < 0) { clearInterval(x); document.getElementById("countdown").innerHTML = "<a href='/register'>Countdown Expired</a>"; } }, 1000); 
 <span id="countdown"></span> 

MDN不鼓励在日期构造函数中使用字符串,因为并非所有浏览器都以相同的方式实现。

如果您确实想使用日期字符串,我建议您使用诸如momentjs的第三方库来解析这些字符串,以确保它在每种浏览器中都有效。

只需标准化日期和时间

 function getNormalisedDatetime(dString) { // yyyy-mm-dd hh:mm:ss var parts = dString.split(" "); var dParts = parts[0].split("-"); var tParts = parts[1].split(":"); return new Date(dParts[0],dParts[1]-1,dParts[2],tParts[0],tParts[1],tParts[2]); } function pad(num) { return ("0"+num).slice(-2); } // Set the date we're counting down to var countDownDate = getNormalisedDatetime("2018-05-25 12: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 days = Math.floor(distance / (1000 * 60 * 60 * 24)); 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); // Display the result in the element with id="countdown" document.getElementById("countdown").innerHTML = "" + pad(days) + " : " + pad(hours) + " : " + pad(minutes) + " : " + pad(seconds); // If the count down is finished, write some text if (distance < 0) { clearInterval(x); document.getElementById("countdown").innerHTML = "<a href='/register'>Countdown Expired</a>"; } }, 1000); 
 <span id="countdown"></span> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM