XCS
XCS

Reputation: 28137

jQuery check mousemover every x ms

I use the function mousemove({}). I want this mousemove function to be called only if it was last called at least 1second ago. I know I may use some kind of timer and put a condition

if( cur_time - last_time > 1)
  do_the_stuff();
  1. Is there a better method of doing this?
  2. If not, what is the function I should use to get cur_time (or to get time passed) ?

Upvotes: 1

Views: 183

Answers (2)

lonesomeday
lonesomeday

Reputation: 237845

That sounds like a good approach. Your code should look something like this:

var last_moved;
$(document).mousemove(function(e){
    if (!last_moved || (e.timeStamp - last_moved > 1000)) {
        do_the_stuff();

        last_moved = e.timeStamp;
    }
});

See a working example on jsFiddle.

Upvotes: 2

Matt Asbury
Matt Asbury

Reputation: 5662

No that seems to be the best way to do it. Put that statement within the mousemove and you should get what you need.

Upvotes: 0

Related Questions