gpt4 book ai didi

javascript - 明年倒计时不会重置

转载 作者:行者123 更新时间:2023-11-28 12:12:09 25 4
gpt4 key购买 nike

我们有圣诞节倒计时,但现在显示已过期,而不是重置到明年

// Set the date we're counting down to
var year = new Date().getFullYear();
var countDownDate = new Date("Dec 24, " + year + " 23:00:00").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

// Get today's date and time
var now = new Date().getTime();

// Find the distance between now and the count down date
var distance = countDownDate - now;

// Time calculations for days, hours, minutes and seconds
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);

// Display the result in the element with id="clock"
document.getElementById("clock").innerHTML = days + " days " + hours + "hrs. " + minutes + "mins. " + seconds + "secs. ";

// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("clock").innerHTML = "EXPIRED";
}
}, 1000);

“过期”消息可能会在 12 月 25 日至 26 日期间显示,然后重置为 27 日直至明年?

最佳答案

我看不到你在哪里检查你的日期是否晚于 12 月 26 日,这里应该是这样的代码

if (days < -2) {
countDownDate = new Date("Dec 24, " + (year + 1) + " 23:00:00").getTime();
} else if (distance < 0) {
clearInterval(x);
document.getElementById("clock").innerHTML = "EXPIRED";
}

或者最好从一开始就写

var now = new Date()
var year = now.getFullYear() + (now.getMonth() == 11 && now.getDate() > 26)

关于javascript - 明年倒计时不会重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59512943/

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