Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect mousemove with jquery

Is there a way to detect when the mouse has stopped moving in jquery?

like image 583
Moshe Avatar asked Jul 12 '26 03:07

Moshe


1 Answers

Yes, use setTimeout and clear it every time when the mouse moves. If the mouse hasn't been moved in the time specified in setTimeout, then you can assume mouse has stopped moving. Utilizing jQuery, you could do something like this:

var stop_timeout = false;
$(function() {
    $().mousemove(function() {
        clearTimeout(stop_timeout);
        stop_timeout = setTimeout(function() {
            alert("The mouse has stopped.");
        }, 1000);            
    });
});

It's a bit heavy to set and unset timeouts every time the mouse moves, but it should work for your purposes.

like image 80
Tatu Ulmanen Avatar answered Jul 14 '26 18:07

Tatu Ulmanen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!