tags:

views:

33

answers:

3

Hi All,

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

A: 

use the hoverIntent plugin or doTimeout (bit heavier)

Moin Zaman
+3  A: 

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.

Nick Craver
@Nick - Man you're a genuis, thanks again
Nasir
Very elegant solution!
elusive
Ninja Dude
@Avinash - `.click()` without parameters is a shortcut for `.trigger('click')` :)
Nick Craver
A: 

You should be able to trigger the click-handler after two seconds. I suggest the following code:

$(function() {
    var timeout;

    $('a.menu-arrow').hover(function() {
        var self = this;
        timeout = setTimeout(function() {
            $(self).click();
            timeout = null;
        });
    }, function() {
        if (timeout) {
            clearTimeout(timeout);
        }
    });
});
elusive