jQuery: is mouse still over the element?

thedp picture thedp · Dec 7, 2009 · Viewed 34.4k times · Source

I would like to be able to detect if the mouse is still over the element in the following scenario:

  1. If mouseover then sleep for a few seconds.
  2. Once done sleeping check of the mouse is still over the same element.
  3. If true then do something.

How can I achieve #2?

Thank you.

Answer

Vilius Paulauskas picture Vilius Paulauskas · Dec 7, 2009

This seems to work (http://jsbin.com/uvoqe)

$("#hello").hover( function () {
  $(this).data('timeout', setTimeout( function () {

    alert('You have been hovering this element for 1000ms');

  }, 1000));
}, function () {

  clearTimeout($(this).data('timeout'));

});