Jason Jason - 1 month ago 5
Javascript Question

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?

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(...);