为什么 JavaScript setTimeout 在另一个选项卡中滞后? [英] Why does JavaScript setTimeout lag when in another tab?

查看:35
本文介绍了为什么 JavaScript setTimeout 在另一个选项卡中滞后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 setTimeout 运行一个 JavaScript 程序,该程序将使用 AJAX 获取外部数据并每 1000 毫秒或一秒更新一次页面内容.

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.

当我的网页所在的选项卡位于最前面时,这非常有效.但是,如果页面在具有多个选项卡的浏览器窗口中运行,而用户当前不在该选项卡中,则 setTimeout 将开始滞后(在一分钟内,而不是更新 60 次,它只更新大约 30 次).

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).

但是,如果网页位于后台浏览器窗口的最前面,则 setTimeout 将继续以正确的速度运行.

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?

推荐答案

浏览器静噪定时器频率当标签不活跃以节省用户的电池寿命.

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

这篇关于为什么 JavaScript setTimeout 在另一个选项卡中滞后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆