setTimeout
accepts two arguments:
callback
- any functiondelay
- 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 functiondelay
- time in ms, which have to pass until nextcallback
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