gpt4 book ai didi

javascript - 如何使用javascript创建分钟倒计时

转载 作者:行者123 更新时间:2023-11-30 15:11:42 25 4
gpt4 key购买 nike

我有一个像网站一样用 html 编写但离线运行的程序,所以我想在用户登录后添加 35 分钟倒计时,然后当 35 分钟结束时,用户将注销

但我无法获得正确的代码,因为我是 javascript 的新手,但我能够让它计数 60 秒,之后它会通过“注销”提醒用户,但我希望它让用户注销不提醒用户注销

这是html代码

 <div id="counter">1:00</div>

这是javascript代码

    function countdown() {
var seconds = 60;
function tick() {
var counter = document.getElementById("counter");
seconds--;
counter.innerHTML = "0:" + (seconds < 10 ? "0" : "") + String(seconds);
if( seconds > 0 ) {
setTimeout(tick, 1000);
} else {
alert("Game over");
}
}
tick();
}
countdown();

最佳答案

    <script>
// Set the date we're counting down to
var countDownDate = new Date("July 12, 2017 09:00:00").getTime();

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

// Get todays date and time
var now = new Date().getTime();

// Find the distance between now an 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);

// Output the result in an element with id="counter"
document.getElementById("counter").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";

// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("counter").innerHTML = "EXPIRED";
}
}, 1000); // time changes in every 1 second
</script>
<p2>to go</p2>

试一试,根据需要更改日期和时间

关于javascript - 如何使用javascript创建分钟倒计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45030673/

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