jQuery check mousemover every x ms
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();
- Is there a better method of doing this?
- If not, what is the function I should use to get cur_time (or to get time passed) ?
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;
}
});
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