Skip to content
Advertisement

Resetting a setTimeout

I have the following:

window.setTimeout(function() {
    window.location.href = 'file.php';
}, 115000);

How can I, via a .click function, reset the counter midway through the countdown?

Advertisement

Answer

You can store a reference to that timeout, and then call clearTimeout on that reference.

// 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(...);
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement