-2

I want to check that user is not using page for some seconds?? Is it possible using javascript ?

Support Team
  • 51
  • 1
  • 1
  • 5
  • 3
    How do you define page usage? Mouse movement? Typing in a form? – João Silva Aug 18 '12 at 16:02
  • document.hasFocus() returns true if document has focus. So it can be used to check if person is still on page. ;) Although it cant be used to check idleness. – ppsreejith Aug 18 '12 at 16:08

3 Answers3

1

You could install a mouse move event handler and a timer. In the mouse move you set a variable with the current time and in the timer you check since how long you din't see any mouse event.

Of course you should also set a keydown handler to see if the user was using the keyboard instead.

Clearly there is no way know if the user was just carefully reading the page... and reading still qualifies as "using" sometimes...

6502
  • 112,025
  • 15
  • 165
  • 265
0

You will have to figure out what you mean by "not in use". One common approach for modern browsers is to see if the page tab has focus. You can use window events focus and blur for this. See also: https://stackoverflow.com/a/6184276/362536

Community
  • 1
  • 1
Brad
  • 159,648
  • 54
  • 349
  • 530
0

Depends on how you define "not in use", but lets say when user switch tab or change browser.

You can capture this evan easy with jQuery. Note that code below is only idea, it may not work without debugging if there is error, but you can see what is my point

var seconds=0;
var notLooking=false;
$(window).blur(function(){
   check();
});
$(window).focus(function(){
   absent=false;
   seconds=0;
});

function check(){
   if( seconds > 10 && absent ){
      alert("dude you should monitor your chat, you are absent too long!!!");
      seconds=0;
      absent=false;
   }else{
     if( absent ){
        setTimeout(check, 1000);
     }else{
       //user is back
       absent=false;
       seconds=0;
     }

   }
}
Milan Jaric
  • 5,556
  • 2
  • 26
  • 34