开发者

execute a javascript function if the user is not active on the page?

i'm looking for a javascript or jQuery func开发者_开发技巧tion to execute my function newalert() if the user is not active on the page, ( maybe with some event to check the cursor position or something like this )

i will make it auto check using settimeout , i just want the function to check the availability of user like in gmail chat, when receiving a new IM and you are not watching the gmail page, it plays a bleep sound.

Thanks


Try using the window.onblur event, or you can have a timer that is running continuously and when the mouse position is the same for x amount of milliseconds, execute the function.

Example:

var lastX, lastY = 0;
var act_timeout = null;
function doAction() {
   alert( 'not active' );
   return false;
}
function move( e ) {
   if( e.clientY == lastY && e.clientX == lastX ) {
       act_timeout = setTimeout( doAction, 5000 );
       return;
   }
   lastX = e.clientX;
   lastY = e.clientY;
   act_timeout = clearTimeout( act_timeout );
}
window.onmousemove = move;
window.onblur = doAction;


You first need to identify what it means for the user to be active and in active. Obvious signs of inactivity include the window blur event, but you might want to look at these others

  • Window blur event
  • No mouse activity: no scrolling, clicks, or movement
  • No keyboard activity

A good list of mouse events and their caveats are provided at Quirksmode.

Once you determine what it means to be inactive, you can clear your timer when the opposing events occur (mouse is moved/clicked/scrolled, window is focused, keyboard event is a received, etc).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜