Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running functions sequentially without nesting the functions - jQuery

Ideally I would like to run these functions sequentially. Formatted like so:

function functionOne() {
    $('.div').animate({'height', 200}, 1000);
}

function functionTwo () {
    // ...
}

$(document).ready(function() {
    functionOne();
    functionTwo();
});

I know I can nest functionTwo inside of functionOne's 'complete' callback function (below), but I'd rather not as it can get a little messy.

function functionOne() {
    $('.div').animate({'height', 200}, 1000, function() {
        functionTwo();
    });
}

Any way around this? Cheers

like image 691
Allan Thomas Avatar asked Jun 22 '26 06:06

Allan Thomas


1 Answers

Maybe you want to use:

function functionOne(callback) {
    $('.div').animate({'height', 200}, 1000, callback);
}

function functionTwo () {
    // ...
}

$(document).ready(function() {
    functionOne(function2);
});

Although in my opinion:

$(function(){
    $('.div').animate({'height', 200}, 1000, function(){
        // ...
    });
};

is more readable (depending on the size of your function2) and does the exact same thing faster.

like image 87
Paul Avatar answered Jun 27 '26 21:06

Paul



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!