我正在为一个客户的网站工作,他需要一个倒数计时器,在每次网站更新两天后倒计时到晚上10点。 (或者他希望进入多天)倒计时在Google Chrome中完美运行,但在Internet Explorer和Firefox中它显示NaN NaN。我读过这意味着“不是数字”所以我的代码在计算日期信息时的方式有问题吗?任何帮助,将不胜感激!我的代码如下:
<span id="countdown1" class="countdown"></span>
<script language="javascript">
// set the date we're counting down to 2 days from now at 10pm
var target_date1 = Date.parse("2014-01-05 22:00:00:000");
// variables for time units
var days1, hours1, minutes1, seconds1;
// get tag element
var countdown1 = document.getElementById("countdown1");
// update the tag with id "countdown" every 1 second
setInterval(function () {
// find the amount of "seconds" between now and target
var current_date1 = new Date().getTime();
var seconds_left1 = (target_date1 - current_date1) / 1000;
// do some time calculations
days1 = parseInt(seconds_left1 / 86400);
seconds_left1 = seconds_left1 % 86400;
hours1 = parseInt(seconds_left1 / 3600);
seconds_left1 = seconds_left1 % 3600;
minutes1 = parseInt(seconds_left1 / 60);
seconds1 = parseInt(seconds_left1 % 60);
// format countdown string + set tag value
countdown1.innerHTML = days1 + " days, " + hours1 + " hours, "
+ minutes1 + " minutes, " + seconds1 + " seconds";
}, 1000);
</script>
答案 0 :(得分:3)
您的日期格式在Firefox(显然是IE)中不起作用。这样:
Date.parse("2014-01-05 22:00:00:000")
Firefox中的是NaN
。 Firefox喜欢"Dec 31, 2013"
之类的日期或"2011-10-10T14:48:00"
之类的ISO日期。 More information at MDN
答案 1 :(得分:3)
将RFC2822或ISO 8601日期格式用于传递给Date.parse()的内容 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
答案 2 :(得分:0)
检查一下你可能会把它当作倒计时..写的是昨天:
http://jsfiddle.net/sf7Fc/ 它会把你的倒数放在选择器中。你可以改变时间的出现方式。
countDown($('#selector'), 300);
<div id="selector"></div>