setInterval(). Both of these functions execute a specified function after a given amount of time either once or repeatedly. While this is straightforward, many people don't realize that the delay is merely a suggestion and can be altered by a number of factors.
Delays are not exact
Apart from engine-related delays, there are a few other factors that play a role in the actual delay of a task. Briefly, these are:
- Browsers throttle nested timeouts and intervals with a delay of at least 4ms, but this can vary depending on the browser.
- Timeouts and intervals in inactive/background tabs are throttled to a minimum of 1000ms to increase battery life.
- Known tracking scripts in background tabs can be throttled even further after a certain amount of time.
On a side note, some browsers store delays as a 32-bit signed integer, meaning that delays over 24.8 days will cause an overflow and execute immediately.
Written by Angelos Chalaris
If you want to keep in touch, follow me on GitHub or Twitter.