views:

71

answers:

1

For some feature, I am working on mousemove event. mousemove event listener is invoked a number of times with in a single linear mouse gesture that is not required. I need to implement a custom event that will be invoked when mouse will stop its motion. I have a guess that it can be implemented on top of mousemove with some delay feature.

Please help me in this regard. Thanks

+2  A: 

You're most of the way 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");
});
Tim Down
Very useful. But how can I pass evt argument to stoppedMoving?
Shoaib
Answer updated to pass event object to `stoppedMoving`.
Tim Down