gpt4 book ai didi

javascript - 一个简单的倒计时器,显示小时、分钟、秒

转载 作者:行者123 更新时间:2023-12-02 16:12:03 26 4
gpt4 key购买 nike

我正在尝试创建一个倒计时器,努力让它显示小时,然后分钟,然后秒。

function startTimer(duration, display) {
var start = Date.now(),
diff,
hours,
minutes,
seconds;

function timer() {
// get the number of seconds that have elapsed since
// startTimer() was called
diff = duration - (((Date.now() - start) / 1000) | 0);

// Setting and displaying hours, minutes, seconds
hours = (diff / 360) | 0;
minutes = (diff / 60) | 0;
seconds = (diff % 60) | 0;

hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;

display.textContent = hours + ":" + minutes + ":" + seconds;

if (diff <= 0) {
// add one second so that the count down starts at the full duration
// example 17:00:00 not 16:59:59
start = Date.now() + 1000;
}
};
// don't want to wait a full second before the timer starts
timer();
setInterval(timer, 1000);
}
window.onload = function () {
var timeLeft = 3600 * 17,
display = document.querySelector('#time');
startTimer(timeLeft, display);
};

努力让分钟和小时正确显示。

除此之外,我还需要计时器在午夜启动并倒计时 17 小时。沿着 3600 * 17(17 小时)的思路思考,然后去掉剩余的持续时间?

var timeLeft = ((3600 * 17) - diff),

最佳答案

您有一个拼写错误和 2 个小计算缺陷:

功能定时器:

 hours   = (diff / 3600) | 0; // was: hours = (diff / 360) | 0;
minutes = ((diff % 3600) / 60) | 0;

onload-Handler:

var hoursMinutesSeconds = 3600 * 17, // was: 360 * 60 * 17; alternative: 60 * 60 * 17
display = document.querySelector('#time');

关于javascript - 一个简单的倒计时器,显示小时、分钟、秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30074348/

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