Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect when mousemove has stopped

Tags:

How is it possible to detect with an eventListener when mousemove has finished?

document.AddEventListener('mousemove', startInteractionTimer, false);  function startInteractionTimer(){   clearInterval(touchInterval);   touchInterval = setInterval(noAction, 6000); } 

I want to start the function startInteractionTimer immediately after the mousemove has ended and I would like to catch that. On the code example above, it is starting if the mouse is moved.

Thanks

Edit: Alright, I answered my own question and the script above --^ is just fine.

like image 759
supersize Avatar asked Feb 25 '13 12:02

supersize


People also ask

How do you check if the mouse is over an element?

You can simply use the CSS :hover pseudo-class selector in combination with the jQuery mousemove() to check whether the mouse is over an element or not in jQuery.

Does MouseMove event bubble?

The MouseMove event is a bubbling event.

How does MouseMove work?

MouseMove is a simple event that is executed when a pointer is moving over or around an element. Mousemove is a javascript event that inside a web page. The mousemove event can also be understood as a part of an event handler, whereupon some action or movement by a mouse pointer, an intended script is executed.


1 Answers

You could always make a custom event for it:

(function ($) {     var timeout;     $(document).on('mousemove', function (event) {         if (timeout !== undefined) {             window.clearTimeout(timeout);         }         timeout = window.setTimeout(function () {             // trigger the new event on event.target, so that it can bubble appropriately             $(event.target).trigger('mousemoveend');         }, 100);     }); }(jQuery)); 

Now you can just do this:

$('#my-el').on('mousemoveend', function () {     ... }); 

Edit:

Also, for consistency with other jQuery events:

(function ($) {     $.fn.mousemoveend = function (cb) {         return this.on('mousemoveend', cb);     }); }(jQuery)); 

Now you can:

$('#my-el').mousemoveend(fn); 
like image 127
Nathan MacInnes Avatar answered Sep 19 '22 05:09

Nathan MacInnes