gpt4 book ai didi

JavaScript 倒计时器到每天的特定时间

转载 作者:行者123 更新时间:2023-12-02 14:37:13 30 4
gpt4 key购买 nike

我目前正在开发一个标题为倒计时器的网站:http://iphone.myhandykey.com/

当前计时器只有 12 小时 + 几分钟。我想要的是倒计时器将显示当前访客所在时区到晚上 11 点为止的剩余时间。那可能吗?谢谢!

Here is the JavaScript:



function startTimer(duration, display) {
var timer = duration, hours, minutes, seconds;
setInterval(function () {



hours = parseInt(((timer / 60) / 60 ) % 60, 10);



minutes = parseInt((timer / 60)%60, 10);
seconds = parseInt(timer % 60, 10);

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

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



if (--timer < 0) {
timer = duration;
}
}, 1000);
}

window.onload = function () {
var onehour = 60 * 600 * 1.231,
display = document.querySelector('#time');
startTimer(onehour, display);
};

这是 HTML:

<span id=time></span>

编辑:如果访客的当前时间是晚上 11:40,则应显示还剩 23 小时和 20 分钟。

最佳答案

(function() {
var start = new Date;
start.setHours(23, 0, 0); // 11pm

function pad(num) {
return ("0" + parseInt(num)).substr(-2);
}

function tick() {
var now = new Date;
if (now > start) { // too late, go to tomorrow
start.setDate(start.getDate() + 1);
}
var remain = ((start - now) / 1000);
var hh = pad((remain / 60 / 60) % 60);
var mm = pad((remain / 60) % 60);
var ss = pad(remain % 60);
document.getElementById('time').innerHTML =
hh + ":" + mm + ":" + ss;
setTimeout(tick, 1000);
}

document.addEventListener('DOMContentLoaded', tick);
})();
Only <span id='time'></span> left!

关于JavaScript 倒计时器到每天的特定时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37358307/

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