Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger a delayed hover event with Jquery

I'm trying to trigger a delayed hover event:

$(".graphic").delay(500).trigger('mouseover').trigger('mouseout');

But the delay is being ignored.

Any ideas?

like image 740
koela1 Avatar asked Nov 19 '11 09:11

koela1


People also ask

Does jQuery handle hover?

jQuery hover() MethodThe hover() method specifies two functions to run when the mouse pointer hovers over the selected elements. This method triggers both the mouseenter and mouseleave events. Note: If only one function is specified, it will be run for both the mouseenter and mouseleave events.

Is jQuery hover deprecated?

Is hover deprecated in jQuery? Deprecated in jQuery 1.8, removed in 1.9: The name “hover” used as a shorthand for the string “mouseenter mouseleave” .

How to call mouseover function in jQuery?

jQuery mouseover() MethodThe mouseover() method triggers the mouseover event, or attaches a function to run when a mouseover event occurs. Note: Unlike the mouseenter event, the mouseover event triggers if a mouse pointer enters any child elements as well as the selected element.


3 Answers

delay() only affects the animation queue, but trigger() is synchronous. You can use queue() to schedule a function triggering the events after the delay:

$(".graphic").delay(500).queue(function(next) {
    $(this).trigger("mouseover").trigger("mouseout");
    next();
});
like image 140
Frédéric Hamidi Avatar answered Oct 12 '22 11:10

Frédéric Hamidi


The .delay() method is best for delaying between queued jQuery effects.

To delay the initial effect, use setTimeout() function. By the way, you could use mouseover() instead of trigger('mouseover')

setTimeout(function () {
  $(".graphic").mouseover().mouseout();
}, 500); 
like image 26
steveyang Avatar answered Oct 12 '22 11:10

steveyang


The jQuery API says:

Only subsequent events in a queue are delayed; for example this will not delay the no-arguments forms of .show() or .hide() which do not use the effects queue.

Maybe you can set a timer, that will trigger mouseover/out after 500 ms using Windows.setTimeout

like image 35
MJ Fathinia Avatar answered Oct 12 '22 10:10

MJ Fathinia