gpt4 book ai didi

javascript - 如何自动重置倒计时?

转载 作者:行者123 更新时间:2023-12-01 00:32:24 25 4
gpt4 key购买 nike

// Set the date we're counting down to
var countDownDate = new Date("Dec 30, 2019 15:37:25").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="demo"
document.getElementById("demo").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";

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

如何重置每年的倒计时?如何在 var countDownDate = new Date("Jan 5, 2021 15:37:25").getTime(); 中使年份动态化,以便您无需编辑 .js 文件每个新年?

一旦 2019 年到期,它就会重新开始并开始计算 2020 年 12 月 30 日,依此类推?

最佳答案

如何根据今天的日期动态获取年份:

// Set the date we're counting down to
dt = new Date();
year = dt.getFullYear();
var countDownDate = new Date(year,11,30,15,37,25).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="demo"
document.getElementById("demo").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";

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

关于javascript - 如何自动重置倒计时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58419420/

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