Reputation: 4528
I am using setTimeout to run a JavaScript program that will use AJAX to fetch external data and update the content of the page once every 1000 milliseconds, or one second.
This works perfectly fine when the tab that my web page is on is in the forefront. However, if the page is running in a browser window with multiple tabs, and the user is not currently in that tab, then the setTimeout will begin to lag (in one minute, rather than updating 60 times, it only updates around 30).
However, if the web page is in the forefront of a browser window that is in the background, then setTimeout will continue to run at the correct speed.
Can someone please explain why this is happening, and any ways to resolve this?
Upvotes: 4
Views: 1871
Reputation: 413702
Browsers squelch timer frequencies when tabs aren't active to save battery life for users.
Upvotes: 4