简体   繁体   中英

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

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

This answere is pretty clean: https://stackoverflow.com/a/18677784/9027171

An excerpt from the answer of infinito84

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM