我有以下几点:window.setTimeout(function() { window.location.href = 'file.php';}, 115000);如何通过.click函数在倒计时中途重置计数器?
3 回答

GCT1015
TA贡献1827条经验 获得超4个赞
您可以存储对该超时的引用,然后调用clearTimeout该引用。
// in the example above, assign the result
var timeoutHandle = window.setTimeout(...);
// in your click function, call clearTimeout
window.clearTimeout(timeoutHandle);
// then call setTimeout again to reset the timer
timeoutHandle = window.setTimeout(...);

烙印99
TA贡献1829条经验 获得超13个赞
您将必须记住超时“ Timer”,将其取消,然后重新启动:
g_timer = null;
$(document).ready(function() {
startTimer();
});
function startTimer() {
g_timer = window.setTimeout(function() {
window.location.href = 'file.php';
}, 115000);
}
function onClick() {
clearTimeout(g_timer);
startTimer();
}
添加回答
举报
0/150
提交
取消