gpt4 book ai didi

javascript - 接下来 5 分钟的剩余时间 - Javascript

转载 作者:数据小太阳 更新时间:2023-10-29 04:40:29 27 4
gpt4 key购买 nike

我正在尝试显示接下来 5 分钟的剩余时间(捕捉到当前时间的完整 5 分钟,例如 15:05、15:10..)

我能够在下一个小时(而不是分钟)的剩余时间内实现相同的目标:

<span class="timer"></span>
<script>
function secondPassed() {
var cur_date = new Date();
var hour = cur_date.getHours();
var minutes = cur_date.getMinutes();
var seconds = cur_date.getSeconds();
var minutes_remain = parseInt(59 - parseInt(minutes));
var seconds_remain = parseInt(60 - parseInt(seconds));

var timers = document.getElementsByClassName('timer');
for (var i = 0; i < timers.length; i++) {
timers[i].innerHTML = minutes_remain+":"+seconds_remain;
}

}

var countdownTimer = setInterval(secondPassed, 1000);
</script>

JSfiddle:http://jsfiddle.net/ov0oo5f7/

最佳答案

像这样吗?

function secondPassed() {
var cur_date = new Date();
var minutes = cur_date.getMinutes();
var seconds = cur_date.getSeconds();

var timers = document.getElementsByClassName('timer');
for (var i = 0; i < timers.length; i++) {
timers[i].innerHTML = (4 - minutes % 5) + ":" + (seconds >= 50 ? "0" : "") + (59 - seconds);
}

}

var countdownTimer = setInterval(secondPassed, 1000);
<span class="timer"></span>

关于javascript - 接下来 5 分钟的剩余时间 - Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26016060/

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