gpt4 book ai didi

javascript - 单击按钮时延长超时功能

转载 作者:行者123 更新时间:2023-11-28 19:42:36 24 4
gpt4 key购买 nike

这段代码有效,但我试图在每次单击按钮时将超时函数重置为“0”。

var running = false,
count = 0,
run_for = 700;


var end_counter = function() {
if (running) {
running = false;
$("#status").text("Not Running");
alert(count);
started_at = 0;
}
};

$('button').click(function() {
if (running) {
count++;


} else {
running = true;
$("#status").text("Running");
count = 1;
setTimeout(end_counter, run_for);
}
});

最佳答案

只需取消并重新启动即可:

var timerId,
count = 0;
function end_counter() {
$("#status").text("Not Running");
alert(count);
count = 0;
}
$('button').click(function() {
$("#status").text("Running");
count++;
clearTimeout(timerId);
timerId = setTimeout(end_counter, 700);
});

关于javascript - 单击按钮时延长超时功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24815842/

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