Search code examples
jqueryjquery-callback

Do something (alert) when all the stuff has finished fading in


I'm trying to figure out how I can do something (alert) when all the stuff has finished fading in. Maybe my syntax is not good?

    $.fn.showdelay = function(){
            var delay = 0;
            return this.each(function(){
                $(this).delay(delay).fadeIn(200);
                delay += 200;
            },
            function(){ 
                alert('done!');
            });
        };
    $item.delay(500).showdelay();

Solution

  • Utilize the callback function parameter in the fadeIn method:

    $.fn.showdelay = function(){
            var delay = 0, count = $(this).length - 1;
    
            return this.each(function(i){
                $(this).delay(delay).fadeIn(200, function() {
                    if(i == count) alert('something');
                });
                delay += 200;
            });
        };
    $item.delay(500).showdelay();
    

    http://api.jquery.com/fadeIn/