Skip to content Skip to sidebar Skip to footer

Get Mouse Coordinates When Mouse Stop Or Change Direction

I get mouse position with simple code: $('#container').mousemove( function(e) { client_x = e.pageX; client_y = e.pageY; // save coordinates }); But I need only

Solution 1:

var timer, timer2 = 0, client_x, client_y; //I made them global since it's easier
$("#container").mousemove( function(e) {
  clearTimeout(timer);
  client_x = e.pageX;
  client_y = e.pageY;
  if((new Date()).getTime() > timer2 + 2000) {
    timer2 = (new Date()).getTime(); //just in case this event handler gets called again before the timer runs doCopy
    setTimeout(doCopy, 1); //run "outside" the event handler (since it's not good for an event handler to take a long time
  } else {
    timer = setTimeout(doCopy, 1000);
  }
});

function doCopy() {
  timer2 = (new Date()).getTime();
  .....
}

This is constantly settings and clearing timers. If the mouse stops for 1 second the doCopy() function gets triggered.


Post a Comment for "Get Mouse Coordinates When Mouse Stop Or Change Direction"