gpt4 book ai didi

javascript - 每次我点击刷新时,倒数计时器都会重新启动

转载 作者:行者123 更新时间:2023-11-29 19:01:43 28 4
gpt4 key购买 nike

每当我刷新浏览器时,它都会从头开始重新启动。我把它上传到服务器上..我得到了同样的结果。这是我的代码:

function getTimeRemaining(endtime) {
var t = Date.parse(endtime) - Date.parse(new Date());
var seconds = Math.floor((t / 1000) % 60);
var minutes = Math.floor((t / 1000 / 60) % 60);
var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
var days = Math.floor(t / (1000 * 60 * 60 * 24));
return {
'total': t,
'days': days,
'hours': hours,
'minutes': minutes,
'seconds': seconds
};
}

function initializeClock(id, endtime) {
var clock = document.getElementById(id);
var daysSpan = clock.querySelector('.days');
var hoursSpan = clock.querySelector('.hours');
var minutesSpan = clock.querySelector('.minutes');
var secondsSpan = clock.querySelector('.seconds');

function updateClock() {
var t = getTimeRemaining(endtime);

daysSpan.innerHTML = t.days;
hoursSpan.innerHTML = ('0' + t.hours).slice(-2);
minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);

if (t.total <= 0) {
clearInterval(timeinterval);
}
}

updateClock();
var timeinterval = setInterval(updateClock, 1000);
}

var deadline = new Date(Date.parse(new Date()) + 33 * 24 * 60 * 60 * 1000);
initializeClock('clockdiv', deadline);
#clockdiv {
font-family: Calibri;
color: #001a00;
display: inline-block;
font-weight: 10;
text-align: center;
font-size: 30px;
}

#clockdiv>div {
padding: 10px;
border-radius: 3px;
display: inline-block;
}

.smalltext {
padding-top: 5px;
font-size: 16px;
}
<div id="clockdiv">
<div>
<span class="days"></span>
<div class="smalltext">Days</div>
</div>
<div>
<span class="hours"></span>
<div class="smalltext">Hours</div>
</div>
<div>
<span class="minutes"></span>
<div class="smalltext">Minutes</div>
</div>
<div>
<span class="seconds"></span>
<div class="smalltext">Seconds</div>
</div>
</div>

这是 fiddle - code

我在这里做错了什么?起初我认为如果我将它上传到服务器上会很好..但仍然得到相同的结果。如果还有其他代码……请帮助我或指导我……怎么做!谢谢!

最佳答案

就是因为这条线

var deadline = new Date(Date.parse(new Date()) + 33 * 24 * 60 * 60 * 1000);

您将日期设置为 future 33 天,因此每次加载页面时都会设置此日期。

如果您将此行更改为以下内容并传入您想要的日期。

var deadline = new Date(Date.parse('2017-10-30'));

运行下面的代码片段。

function getTimeRemaining(endtime) {
var t = Date.parse(endtime) - Date.parse(new Date());
var seconds = Math.floor((t / 1000) % 60);
var minutes = Math.floor((t / 1000 / 60) % 60);
var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
var days = Math.floor(t / (1000 * 60 * 60 * 24));
return {
'total': t,
'days': days,
'hours': hours,
'minutes': minutes,
'seconds': seconds
};
}

function initializeClock(id, endtime) {
var clock = document.getElementById(id);
var daysSpan = clock.querySelector('.days');
var hoursSpan = clock.querySelector('.hours');
var minutesSpan = clock.querySelector('.minutes');
var secondsSpan = clock.querySelector('.seconds');

function updateClock() {
var t = getTimeRemaining(endtime);

daysSpan.innerHTML = t.days;
hoursSpan.innerHTML = ('0' + t.hours).slice(-2);
minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);

if (t.total <= 0) {
clearInterval(timeinterval);
}
}

updateClock();
var timeinterval = setInterval(updateClock, 1000);
}

var deadline = new Date(Date.parse('2017-10-30'));
initializeClock('clockdiv', deadline);
#clockdiv {
font-family: Calibri;
color: #001a00;
display: inline-block;
font-weight: 10;
text-align: center;
font-size: 30px;
}

#clockdiv>div {
padding: 10px;
border-radius: 3px;
display: inline-block;
}

.smalltext {
padding-top: 5px;
font-size: 16px;
}
<div id="clockdiv">
<div>
<span class="days"></span>
<div class="smalltext">Days</div>
</div>
<div>
<span class="hours"></span>
<div class="smalltext">Hours</div>
</div>
<div>
<span class="minutes"></span>
<div class="smalltext">Minutes</div>
</div>
<div>
<span class="seconds"></span>
<div class="smalltext">Seconds</div>
</div>
</div>

关于javascript - 每次我点击刷新时,倒数计时器都会重新启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46471055/

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