- setTimeout(function, milliseconds)
- setInterval(function, milliseconds)
If you want something to happen one time after a specified period of time, then use setTimeout(). That is because it only executes one time when the specified interval is reached.
If you want to loop code for animations or on a clock tick, then use setInterval().
Accuray (setTimeout Vs. setInterval)
Please note that all scheduling methods do not guarantee the exact delay. For example, the in-browser timer may slow down for a lot of reasons:
- The CPU is overloaded.
- The browser tab is in the background mode.
- The laptop is on battery.