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 second so as to prevent being overwhelmed whenever i move my mouse.
Currently, i have the code below, which is logging the mouse position everytime it moves, BUT is not throttling it and i can't seem to figure out what's going wrong
//Code runs after document is ready
function logMouse(event){
console.log('The mouse is currently at ('+event.pageX+','+event.pageY+')');
}
$(document).on('mousemove',function(event){
setTimeout(function(){
logMouse(event);
},200);
});
I am trying to throttle the mousemove events by using setTimeout, and setting the timer to 200 mse so that it would fire 5 times in 1 second, but my code isn't working and is currently just giving me a mass of mouse positions whenever i move my mouse.
How do i throttle my mousemove so that it performs the logging of the mouse's position no more than 5 times a second?