重置setTimeout

我有以下几点:


window.setTimeout(function() {

    window.location.href = 'file.php';

}, 115000);

如何通过.click函数在倒计时中途重置计数器?


慕少森
浏览 802回答 3
3回答

GCT1015

您可以存储对该超时的引用,然后调用clearTimeout该引用。// in the example above, assign the resultvar timeoutHandle = window.setTimeout(...);// in your click function, call clearTimeoutwindow.clearTimeout(timeoutHandle);// then call setTimeout again to reset the timertimeoutHandle = window.setTimeout(...);

烙印99

您将必须记住超时“ 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();}
打开App,查看更多内容
随时随地看视频慕课网APP