Jacob
Jacob

Reputation: 2071

Run function when other function is completed

Im looking for a way to run a function when the other function is ready.

This is the function:

$(".box").first().show(150, function showNext () {

    $(this).next(".box").show(150, showNext);

});

So Im look for a way to run another function after that, for example:

alert("Done");

I've tried this but didn't work.

$(".box").first().show(150, function showNext () {

    $(this).next(".box").show(150, showNext);

}, function () {

alert("Done");

});

What I want is, in words:

if(all .box is visble or if showNext is idle/completed){

alert("Done");

}

I appreciate any help! Thanks.

Upvotes: 0

Views: 53

Answers (1)

evanjdooner
evanjdooner

Reputation: 904

How about :

$(".box").first().show(150, function showNext () {
    var next = $(this).next(".box");

    if (next.length > 0) { // If a next sibling with class "box" was found
        next.show(150, showNext);
    } else { // No sibling found, end reached
        alert("Done");
    }
});

Upvotes: 1

Related Questions