window.setTimout()
returns a TimeoutID
, which can be used to stop that timeout from running. To do this, store the return value of window.setTimeout()
in a variable and call clearTimeout()
with that variable as the only argument:
function waitFunc(){
console.log("This will not be logged after 5 seconds");
}
function stopFunc(){
clearTimeout(timeout);
}
var timeout = window.setTimeout(waitFunc,5000);
window.setTimeout(stopFunc,3000);
This will not log the message because the timer is stopped after 3 seconds.