Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setTimeout in a JQuery animation

I'm having a problem with the setTimeout(). I want, in the mouseout state, that the submenu slides Up after a interval (500 miliseconds). But the setTimeout() isn't working.

Like in this link: http://jsfiddle.net/felipepalazzo/Xyhvn/2/

The code:

(function($){
    $.fn.showMenu = function(options){

        var settings = $.extend({
            height  : '40px',
            speed   : '500',
            heightx : '20px'              
        }, options || {});

        return this.each(function(){
           var elem = $(this);
           var menu_timer;
            elem.hover(function(){
                $(this).stop().animate({'height' : settings.height}, settings.speed);
                    }, function(){
                      //setTimeout(function(){  
                      $(this).stop().animate({'height' : settings.heightx}, settings.speed);
                            //},500);
                    }); 
        });      
    };
})(jQuery);
like image 415
Felipe Palazzo Avatar asked Jul 03 '26 12:07

Felipe Palazzo


2 Answers

This is out of scope.

var that = this;
setTimeout(function(){
    $(that).stop().animate({'height' : settings.heightx}, settings.speed);
},500);
like image 108
Nick Caballero Avatar answered Jul 06 '26 02:07

Nick Caballero


Use delay()

So for example

$(this).delay(500).stop().animate({'height' : settings.heightx}, settings.speed);
like image 39
Jonas Røssum Avatar answered Jul 06 '26 02:07

Jonas Røssum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!