3

I have a function, where I define lots of setTimeout() functions . When user presses the button, it's starting the execution.

How can I cancel it ?

Is there any way to cancel ALL timeouts (assuming that I have multiple without the name)

4 Answers 4

4
// start timer
var timer = setTimeout(...);

// cancel timer
clearTimeout(timer);

See https://developer.mozilla.org/en-US/docs/DOM/window.clearTimeout

Sign up to request clarification or add additional context in comments.

Comments

0

Pass the timer ID returned by setTimeout to clearTimeout.

Comments

0

Use clearTimeout:

var timer = setTimeout(function(){ alert("hello!"); }, 3000);   

...

clearTimeout(timer);

Comments

0

The function setTimeout() returns an ID you can use to clear the timeout. You'll have to keep track of the timeouts, and clear them manually:

var timeoutID = setTimeout( scheduledFunction, time );

// do stuff...

//clear the timeout:
clearTimeout( timeoutID  );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.