开发者

Running functions sequentially without nesting the functions - jQuery

开发者 https://www.devze.com 2023-03-23 12:33 出处:网络
Ideally I would like to run these fun开发者_开发技巧ctions sequentially. Formatted like so: function functionOne() {

Ideally I would like to run these fun开发者_开发技巧ctions 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消