如何在javascript中过期后重新启动coundown计时器24小时



我正在使用倒计时计时器,该计时器每天运行24小时。到目前为止,它运行得很好,但当时间为零时,我希望它重新启动计时器24小时。

当用户提交付款时,我所做的是将存款时间保存在unix时间戳中,并用它运行24小时的倒计时,每天给他1%,但我无法每天运行它。

到目前为止我所做的:

var deposit_time = user.deposit_time;
var countDownDate = new Date(deposit_time * 1000).getTime() + 86400000;
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
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);
new Date(Date.now() + (3600 * 1000 * 24))

document.getElementById("next_income_countdown").innerHTML =
hours + "h " +
minutes + "m " + seconds + "s ";
if (distance < 0) {
clearInterval(x);
document.getElementById("next_income_countdown").innerHTML = '00:00:00';
// run for another 24 hrs
}
}, 1000);

distance为零时,只需将他人一天的ms86400000添加到countDownDate即可;重置";it:

var deposit_time = user.deposit_time;
var countDownDate = new Date(deposit_time * 1000).getTime();
var now = new Date().getTime();
// adjust countDownDate if there have been multiple days since deposit
while (now >= countDownDate) {
countDownDate += 86400000;
}
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
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);
document.getElementById("next_income_countdown").innerHTML = hours + "h " + minutes + "m " + seconds + "s ";
if (distance =< 0) {
document.getElementById("next_income_countdown").innerHTML = '00:00:00';
// run for another 24 hrs
countDownDate += 86400000;
}
}, 1000);

最新更新