当页面/选项卡处于非活动状态时如何停止 setInterval 自动刷新?
我有一个 javascript 来解析 twitter feed 并每 30 秒在我的页面中的一个块中显示它们,代码是这样的:
var auto_refresh = setInterval( function () { //get twitter feeds }); }, 30000);
现在,如果用户最小化他的浏览器或切换到另一个选项卡(页面不活动),我想禁用此 auto_referesh clearInterval(auto_refresh);
感谢您的帮助
i have a javascript to parse twitter feeds and show them in a block in my page every 30 seconds, the code is something like this:
var auto_refresh = setInterval( function () { //get twitter feeds }); }, 30000);
now in case the user minimized his browser or switched to another tab (page is not active) i want to disable this auto_referesh clearInterval(auto_refresh);
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
答案如下:
有没有办法检测浏览器窗口当前是否处于活动状态?
Here is the answer:
Is there a way to detect if a browser window is not currently active?
您可能想查看
另请注意,Chrome 显然有一些会减慢速度的优化当选项卡未处于焦点时触发计时器:我怎样才能设置setInterval Chrome 中的标签处于非活动状态时可以工作吗?
You may want to check out
Also note that Chrome apparently has some optimizations that slow down the firing of the timer when the tab is not in focus: How can I make setInterval also work when a tab is inactive in Chrome?
这个答案非常干净:https://stackoverflow.com/a/18677784/9027171
摘自<的答案a href="https://stackoverflow.com/users/2661609/infinito84">infinito84
This answere is pretty clean : https://stackoverflow.com/a/18677784/9027171
An excerpt from the answer of infinito84