gpt4 book ai didi

javascript - 计数器加速

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

我不明白为什么每次单击“开始”时此脚本都会使计数器加速。我需要插入什么来防止这种情况发生?

var count = 1;
var counting = function () {
timer = setInterval(function () {
$('#numbers').html(count);
count++;
}, 1000);
function counting() {};
}
$("#start").click(counting);
$('#stop').click(function () {
clearTimeout(timer);
});
$('#reset').click(function () {
count = 0;
$('#numbers').html(count);
});

最佳答案

您需要将 setIntervalclearInterval 匹配,而不是 clearTimeout

如果计时器已经启动,您还需要防止对 counting 的额外调用,例如通过在 counting 中放置一个 $('#start').prop('disabled', true) 调用。

或者,只需确保在调用 timer = setInterval(...) 之前立即调用 clearInterval(timer)

关于javascript - 计数器加速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16989521/

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