这段代码有效,但我试图在每次单击按钮时将超时函数重置为“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/