I have an event handler bound to the mousemove handler which will log the current mouse position via console.log().I am aiming to have the event fire no more than 5 times a
Add a variable that tracks when an event has just occurred, and sleep with setTimeout
before allowing the next event.
var timesPerSecond = 5; // how many times to fire the event per second
var wait = false;
$(document).on('mousemove', function (event) {
// don't handle events when one has just occurred
if (!wait) {
// fire the event
logMouse(event);
// stop any further events
wait = true;
// after a fraction of a second, allow events again
setTimeout(function () {
wait = false;
}, 1000 / timesPerSecond);
}
});