-1

In my webpage there is online status like available,idle,busy etc.If we didn't load the page until 10 minutes the status get changed to available to idle.Once i return back to my webpage tab browser, how can create an event that used to change status idle to available unless refreshing the page.

jithin
  • 459
  • 8
  • 21
Sandeep Nambiar
  • 1,656
  • 3
  • 22
  • 38
  • 1
    Research keywords: [Page Visibility API](http://www.w3.org/TR/page-visibility/), `visibilitychange` event. – CBroe Feb 22 '15 at 14:03
  • and for older than IE10 I think you can use `window.onfocus` and `window.onblur` – Kaiido Nov 04 '15 at 05:40
  • Wanted to add as an answer but doesn't really make sense to post something which was already discussed on SO [here](http://stackoverflow.com/questions/28993157/visibilitychange-event-is-not-triggered-when-switching-program-window-with-altt) (read the OP completely). Other related: [How can I tell when a tab/window gains focus](http://stackoverflow.com/q/9365576/1369473) and [Detect If Browser Tab Has Focus](http://stackoverflow.com/q/7389328/1369473) – Fr0zenFyr Nov 04 '15 at 05:52

1 Answers1

0

This could be done using the mouseover event on the body element.

document.getElementsByTagName('body')[0].addEventListener("mouseover", function(){
   // If idle then set status to available

});

EDIT:

Here's an idea. The Window.requestAnimationFrame() function is only executed when the tab/window is active. So if the function is not called, that means the user is not viewing the tab or the tab is not active.

Check this example. Run the script, then switch tabs for a few seconds, then switch back

var isTabOpen=true;
var tabTimer=null;



function tabCheck(){
  //This function is only called when the tab is open.
  
  if(tabTimer) clearTimeout(tabTimer);
  
    tabTimer = setTimeout(function(){
      isTabOpen = false;
    },1000);
  
  isTabOpen=true;
   
}

setInterval(function(){
  window.requestAnimationFrame(tabCheck);
  document.getElementsByTagName('body')[0].innerHTML += (isTabOpen+'<br/>');
},1000);
Jackson
  • 3,476
  • 1
  • 19
  • 29