Hi, I'm Jacob. Enjoying devFlipCards? Buy me a coffee

10. How to stop interval or timeout?

setTimeout accepts two arguments:

  • callback - any function
  • delay - delay in ms.

Function callback will be executed not sooner than after delay time. It can be executed later, if synchronical code takes longer to finish. It comes from JavaScript and Event Loop properties.

setInterval accepts two arguments:

  • callback - any function
  • delay - time in ms, which have to pass until next callback execution.

Similarly to setTimeout, callback will be executed no sooner than before delay time passes..

Either setTimeout and setInterval return ids, which can be used to stop timeout or interval. To do this, two functions are needed: clearTimeout and clearInterval.

const timeout = setTimeout(() => {}, 100); const interval = setInterval(() => {}, 100); clearTimeout(timeout); // timeout won't execute clearInterval(interval); // interval will stop and callback won't execute
Struggling to find common date to meet with your friends? Try our new tool
commondate.xyz