2

I've got some troubles with setTimeOut, i want to delay the hide of the submenu, but its not working at all.. :-(

I put the working line inside comments. What i have done wrong?

        $('ul.menu').find('li').mouseover(function() {
           $(this).children('ul, .mega-menu').css('display','block');

        }).mouseout(function() {
           setTimeout(this.children('ul, .mega-menu').style.display="none",4000); 
           /* $(this).children('ul, .mega-menu').css('display','none'); */
        });

Thanks for any help or ideas!

4
  • 1
    try setTimeout(this.children('ul, .mega-menu').hide(4000); Commented Oct 23, 2015 at 12:33
  • setTimeout(function(){ this.children('ul, .mega-menu').style.display="none"}, 4000); Commented Oct 23, 2015 at 12:40
  • you just needed to pass an anon callback function to setTimeout method Commented Oct 23, 2015 at 12:42
  • 2
    @MayankVadiya: That's not even valid JavaScript.... Commented Oct 23, 2015 at 12:43

4 Answers 4

1

Please use this to hide with delay:-

$('ul.menu').find('li').delay(4000).fadeOut();   

This will wait for 4 sec then hide

To show use this:-

$('ul.menu').find('li').delay(6000).fadeIn();

So you can write like this:-

$(document).ready(function(){    
  $('ul.menu').find('li').hover(function() {
     var $megamenu = $(this).children('ul, .mega-menu'); 
     $megamenu.show();   
  },function() {
    var $megamenu = $(this).children('ul, .mega-menu');  
    $megamenu.delay(4000).fadeOut(); 
 });
});

you can also use fadeOut(1000) for animation effect.

If you want to use setTimeout you can use Dave Salomon answers or this:-

$(document).ready(function(){    
  $('ul.menu').find('li').hover(function() {
     var $megamenu = $(this).children('ul, .mega-menu'); 
     $megamenu.show();   
  },function() {
    var $megamenu = $(this).children('ul, .mega-menu');  
    setTimeout(function(){$megamenu.hide()},"4000"); 
 });
});

Hope this will help.

Sign up to request clarification or add additional context in comments.

2 Comments

As much as your answer works and improves on a better way to handle what he was trying to do while sticking in the context of jquery, this doesn't answer what he did wrong when using setTimeout, which is what he asked.
Awesome! Just so original questioner or someone searching knows why it didn't work the way they tried. +1
0

Try this.

$('ul.menu').find('li').mouseover(function() {
    $(this).children('ul, .mega-menu').show();
}).mouseout(function() {
    $(this).children('ul, .mega-menu').hide(4000);     
});

Comments

0

You need to pass either a function (and not calling it) or code to setTimeout's first parameter. Try:

var that = this;
setTimeout(function(){
    that.children('ul, .mega-menu').style.display="none";
}, 4000); 

Comments

0

Your scope in your setTimeout(function(){...}); isn't the same as the rest of the method. i.e. this is different.

Also, your code is a bit wonky.

$('ul.menu').find('li').mouseover(function() {
    var $submenu = $(this).children('ul, .mega-menu');
    $submenu.css('display','block');
}).mouseout(function() {
    var $submenu = $(this).children('ul, .mega-menu');
    setTimeout(function(){
        $submenu.hide();
    }, 4000);
});

http://jsfiddle.net/daveSalomon/hLk0s0bd/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.