簡體   English   中英

當頁面/選項卡處於非活動狀態時如何停止 setInterval 自動刷新?

[英]how to stop setInterval auto-refresh when page/tab is inactive?

我有一個 javascript 來解析 twitter 提要並每 30 秒在我的頁面的一個塊中顯示它們,代碼是這樣的:

var auto_refresh = setInterval(
function ()
    {
        //get twitter feeds
    });
}, 30000);

現在,如果用戶最小化他的瀏覽器或切換到另一個選項卡(頁面未激活),我想禁用此 auto_referesh clearInterval(auto_refresh);

謝謝你的幫助

這個答案很干凈: https://stackoverflow.com/a/18677784/9027171

摘自infinito84的答案

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM