Javascript detects user viewing page

At present, my site keeps automatically reloading the javascript function every 10 seconds. setInterval( "checklatestmsgidLive();", 10000 );I think that the function will continue to reload the function anyway, even when the user views another site in a different tab. How to make it reload the function only when the user browses my site? Can javascript be detected if a user browses my site?

I saw that the wall of facebook.com also updates new posts only when I view the page, when I am on a different tab, it will not update new posts. How to do it?

+3
source share
3 answers
var tId, idleTimer;
function initReload() { 
  clearInterval(tId);
  tId = setInterval(checklatestmsgidLive, 10000 );

}
window.onload=window.onfocus=function() {
  initReload()
}
window.onblur=function() {
  clearInterval(tId);
}
window.onmousemove=function() {
  clearTimeout(idleTimer);
  idleTimer = setTimeout(function() { clearInterval(tId);},600000); // idle for 10 minutes
}
+6

- .

+5

, . W3C API: . , , , , Google Chrome (Dev Channel) Internet Explorer 10 Platform Preview.

- , . - , Chrome . , , , .

IE- , Chrome Dev IE 10.

+5
source

All Articles