gpt4 book ai didi

Javascript Countdown 在 IE 和 Firefox 中显示 NaN,在 Chrome 中有效

转载 作者:行者123 更新时间:2023-11-29 16:13:44 25 4
gpt4 key购买 nike

我正在为客户开发一个网站,他需要一个倒计时计时器,以便从每次网站更新后两天倒计时到晚上 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>

最佳答案

您的日期格式在 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 .

关于Javascript Countdown 在 IE 和 Firefox 中显示 NaN,在 Chrome 中有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20915230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com