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
devFlipCards 2024

Do you accept cookies?

Cookies are small amounts of data saved locally on you device, which helps our website - it saves your settings like theme or language. It helps in adjusting ads and in traffic analysis. By using this site, you consent cookies usage.