Possible Duplicate:
Is there a way to detect if a browser window is not currently active?
我有一个每秒调用一次的函数,我只想在当前页面位于前台时运行它,即用户没有最小化浏览器或切换到另一个选项卡。如果用户不看它并可能占用大量 CPU 资源,它就毫无用处,所以我不想只是在后台浪费周期。
有人知道如何在 JavaScript 中说明这一点吗?
注意:我使用 jQuery,所以如果你的答案使用它,那很好:)。
最佳答案
除了Richard Simões回答你也可以使用Page Visibility API .
if (!document.hidden) {
// do what you need
}
This specification defines a means for site developers to programmatically determine the current visibility state of the page in order to develop power and CPU efficient web applications.
了解详情(2019 年更新)
关于javascript - 如何判断浏览器/选项卡是否处于事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1760250/