【问题标题】:how to stop setInterval auto-refresh when page/tab is inactive?当页面/选项卡处于非活动状态时如何停止 setInterval 自动刷新?
【发布时间】:2011-09-04 23:29:05
【问题描述】:

我有一个 javascript 来解析 twitter 提要并每 30 秒在我的页面中显示它们,代码是这样的:

var auto_refresh = setInterval( 功能 () { //获取推特信息 }); }, 30000);

现在如果用户最小化他的浏览器或切换到另一个选项卡(页面未激活)我想禁用这个 auto_referesh clearInterval(auto_refresh);

感谢您的帮助

【问题讨论】:

标签: javascript jquery setinterval


【解决方案1】:

这个答案很干净:https://stackoverflow.com/a/18677784/9027171

摘自infinito84的回答

 if(!document.hasFocus()){
       // The tab is not active
 }
    else{
       // The tab is active
 } 

【讨论】:

    【解决方案2】:
    【解决方案3】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    • 2021-02-15
    相关资源
    最近更新 更多