gpt4 book ai didi

javascript - 倒计时不起作用

转载 作者:行者123 更新时间:2023-11-28 08:16:16 26 4
gpt4 key购买 nike

我有一个脚本,我在其中尝试进行倒计时,倒计时 30 天,然后重置为 30 天并重新开始。但我的问题是,当我在我的浏览器 (safari) 中打开它时,页面是空白的,上面没有任何内容,尽管如果我删除我用来重置倒计时的功能,它可以工作,但我需要它重置。

if (seconds_left <= 0){
target_date = target_date + 30 days;
}

完整代码:

 <!DOCTYPE html>


<html>

<head>
</head>

<body>

<span id="countdown"></span>

<script LANGUAGE="Javascript">

var target_date = new Date("Apr 9, 2015").getTime();

var days, hours, minutes, seconds;

var countdown = document.getElementById("countdown");


if (seconds_left <= 0){
target_date = target_date + 30 days;
}

setInterval(function () {

var current_date = new Date().getTime();
var seconds_left = (target_date - current_date) / 1000;

days = parseInt(seconds_left / 86400);
seconds_left = seconds_left % 86400;

hours = parseInt(seconds_left / 3600);
seconds_left = seconds_left % 3600;

minutes = parseInt(seconds_left / 60);
seconds = parseInt(seconds_left % 60);

countdown.innerHTML = days + "d, " + hours + "h, "
+ minutes + "m, " + seconds + "s";

}, 1000);

</script>

</body>

</html>

最佳答案

<!DOCTYPE html>
<html>
<head></head>
<body>
<span id="countdown"></span>

<script type="text/javascript">

var target_date = new Date("Apr 9, 2015").getTime();

var days, hours, minutes, seconds;

var countdown = document.getElementById("countdown");

setInterval(function () {

var current_date = new Date().getTime();
var seconds_left = (target_date - current_date) / 1000;

//the following two lines are moved inside the function
if (seconds_left <= 0)
{
target_date = target_date + 30; //just 30 would suffice to add 30 days
seconds_left = (target_date - current_date) / 1000; //then update the seconds_left to continue
}

days = parseInt(seconds_left / 86400);
seconds_left = seconds_left % 86400;

hours = parseInt(seconds_left / 3600);
seconds_left = seconds_left % 3600;

minutes = parseInt(seconds_left / 60);
seconds = parseInt(seconds_left % 60);

countdown.innerHTML = days + "d, " + hours + "h, "
+ minutes + "m, " + seconds + "s";

}, 1000);
</script>
</body>
</html>

关于javascript - 倒计时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28974654/

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