3

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

Alaa
  • 4,471
  • 11
  • 50
  • 67
  • possible duplicate of [Is there a way to detect if a browser window is not currently active?](http://stackoverflow.com/questions/1060008/is-there-a-way-to-detect-if-a-browser-window-is-not-currently-active) – no.good.at.coding May 30 '11 at 17:57

3 Answers3

3

Here is the answer:

Is there a way to detect if a browser window is not currently active?

Community
  • 1
  • 1
Naveed Ahmad
  • 3,176
  • 1
  • 15
  • 18
0

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
 } 
Abdelhadi Abdo
  • 392
  • 2
  • 9