Alaa
Alaa

Reputation: 4611

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

Upvotes: 3

Views: 10965

Answers (3)

Abdelhadi Abdo
Abdelhadi Abdo

Reputation: 412

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
 } 

Upvotes: 0

Related Questions