Why does JavaScript setTimeout lag when in another tab?

戏子无情 提交于 2019-12-11 01:46:05

问题


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?


回答1:


Browsers squelch timer frequencies when tabs aren't active to save battery life for users.



来源:https://stackoverflow.com/questions/19328140/why-does-javascript-settimeout-lag-when-in-another-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!