Question

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) ?
Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top