Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have a mouseover event fire only if the mouse is hovered over an element for at least 1 second?

I want to display a dialog when a user mouses over a certain image. That part works. Unfortunately if the mouse even just passes over the corner of the image quickly it will display the dialog. I would like to have the dialog show only if the mouse is left over the image for one full second so as to avoid inadvertent pop ups.

I saw this question but it is for jQuery and I am using Prototype. I don't know enough jQuery to interpret that solution.

If someone could explain the logic or JavaScript functionality that will be required to cause a delayed firing of the mouseover event I would appreciate it.

like image 237
John Conde Avatar asked Jun 03 '11 18:06

John Conde


People also ask

What is the mouseover event fired?

The mouseover event is fired at an Element when a pointing device (such as a mouse or trackpad) is used to move the cursor onto the element or one of its child elements.

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.

Which event triggers when we bring mouse cursor over any element?

The mouseover event occurs when a mouse pointer comes over an element, and mouseout – when it leaves.


1 Answers

You can't delay the firing of the event, but you can delay your handling of the event.

Here's a quick example without jQuery or Prototype that will make it easier to understand.

var delay = function (elem, callback) {     var timeout = null;     elem.onmouseover = function() {         // Set timeout to be a timer which will invoke callback after 1s         timeout = setTimeout(callback, 1000);     };      elem.onmouseout = function() {         // Clear any timers set to timeout         clearTimeout(timeout);     } };   delay(document.getElementById('someelem'), function() {     alert("Fired"); }); 
like image 123
Robert Avatar answered Oct 06 '22 03:10

Robert