I want to check that user is not using page for some seconds?? Is it possible using javascript ?
-
3How 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 Answers
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...

- 112,025
- 15
- 165
- 265
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
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;
}
}
}

- 5,556
- 2
- 26
- 34