Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I trigger a click when hovering over a element for 2 seconds, using JQuery?

Tags:

jquery

How do I trigger this inline function

onClick="showMenu('mnu_searches', event, 0, this)

using JQuery...if I hover over a.menu-arrow ? I need to trigger a click after the user has been hovering over the element for 2 seconds?

Any help would be greatly appreciated, Thanks

like image 541
Nasir Avatar asked Sep 07 '10 12:09

Nasir


1 Answers

You can create and clear a 2 second timer, like this:

$("a.menu-arrow").hover(function() {
  $.data(this, "timer", setTimeout($.proxy(function() {
    $(this).click();
  }, this), 2000));
}, function() {
  clearTimeout($.data(this, "timer"));
});

You can give it a try here. By using $.data() we're storing a timeout per element to avoid any issues and clear the correct timer. The rest is just setting a 2 second timer when entering the element, and clearing it when leaving. So if you stay for 2000ms, it fires a .click(), if you leave it stops clear the timer.

like image 67
Nick Craver Avatar answered Dec 08 '22 10:12

Nick Craver