I am trying to find a clean way to aggregate mousemove events, so that I ensure that my code is called, but only once every 250-300 milliseconds.
I thought about using something like the following, but wondered if there was a better pattern, or something that provides jQuery that would do the same:
var mousemove_timeout = null; $('body').mousemove(function() { if (mousemove_timeout == null) { mousemove_timeout = window.setTimeout(myFunction, 250); } }); function myFunction() { mousemove_timeout = null; }
EDIT: The answer below will work fine for this situation, however, I found that the mousestop() functionality provided in the answer actually eliminated my aggregation need, so if you read this question and look for the answer, see if mousestop is really needed plugin
Topher fangio
source share