gpt4 book ai didi

javascript - 分钟和秒的 jQuery 倒数计时器

转载 作者:数据小太阳 更新时间:2023-10-29 03:58:42 25 4
gpt4 key购买 nike

我想创建一个 jquery 倒数计时器,我尝试了以下代码但它不起作用。我该怎么办?

演示: https://jsfiddle.net/tbosn210/

var interval = setInterval(function() {

var timer2 = $("5:01");
var timer = timer2.split(':');
//by parsing integer, I avoid all extra string processing
var minutes = parseInt(timer[0],10);
var seconds = parseInt(timer[1],10);
--seconds;
minutes = (seconds < 0) ? --minutes : minutes;
if (minutes < 0) clearInterval(interval);
seconds = (seconds < 0) ? 59 : seconds;
seconds = (seconds < 10) ? '0' + seconds : seconds;
//minutes = (minutes < 10) ? minutes : minutes;
$('.countdown').html(minutes + ':' + seconds);
}, 1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="countdown"></div>

最佳答案

将您的 timer2 声明移出 setInterval 函数,在执行结束时为 timer2 分配新的时间值以继续。

工作片段:

var timer2 = "5:01";
var interval = setInterval(function() {


var timer = timer2.split(':');
//by parsing integer, I avoid all extra string processing
var minutes = parseInt(timer[0], 10);
var seconds = parseInt(timer[1], 10);
--seconds;
minutes = (seconds < 0) ? --minutes : minutes;
if (minutes < 0) clearInterval(interval);
seconds = (seconds < 0) ? 59 : seconds;
seconds = (seconds < 10) ? '0' + seconds : seconds;
//minutes = (minutes < 10) ? minutes : minutes;
$('.countdown').html(minutes + ':' + seconds);
timer2 = minutes + ':' + seconds;
}, 1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="countdown"></div>

关于javascript - 分钟和秒的 jQuery 倒数计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41035992/

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