Java Script: mousestop event

For some function, I am working on the mousemove event. The mousemove event listener is called several times with one mouse gesture, which is not required. I need to implement a custom event that will be raised when the mouse stops its movement. I assume that it can be implemented on top of mousemove with some delay function.

Please help me in this regard. Thanks

+3
source share
1 answer

You are most there:

function waitForMouseStop(callback) {
    var timer;

    function stoppedMoving(evt) {
        document.onmousemove = null;
        callback();
    }

    function moveHandler(evt) {
        evt = evt || window.event;
        if (timer) {
            window.clearTimeout(timer);
        }
        timer = window.setTimeout(function() {
            stoppedMoving(evt);
        }, 500);
    }

    document.onmousemove = moveHandler;
}

waitForMouseStop(function() {
    alert("Stopped");
});
+4
source

Source: https://habr.com/ru/post/1754939/


All Articles