1

I have one drop down menu,

<ul>
<li><a>link 1</a>
<ul><li><a>link 1</a></li></ul>
</li>
</ul>

I am using the following JS to use hover and show child menus.

I want to add delay to the mouse out function (when the class of the LI removed) about 500ms,

 $('li').hover(function(){
    $(this).addClass('over');
    }, function(){
    $(this).removeClass('over');
  });

Please do needful in this.

thanks in advance

Wasim Shaikh
  • 6,880
  • 18
  • 61
  • 88
  • possible duplicate of [Delay jquery hover event?](http://stackoverflow.com/questions/435732/delay-jquery-hover-event) – zaf May 10 '10 at 09:25

2 Answers2

7

You can do something like this:

$('li').hover(function(){
  var timer = $(this).data('timer');
  if(timer) clearTimeout(timer);
  $(this).addClass('over');
}, function(){
  var li = $(this);
  li.data('timer', setTimeout(function(){ li.removeClass('over'); }, 500));
});

This clears any timeout when hovering over it (in case you hover, leave, hover back you need to check this) and sets a 500ms delay when leaving the hover, storing the timeout ID on the li itself using .data().

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
1
$('li').hover(function(){
    $(this).addClass('over');
    }, function(){
    setTimeout(function(){$(this).removeClass('over')}, 500);
  });
sundowatch
  • 3,012
  • 3
  • 38
  • 66