3

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

3
  • what is messy about nesting functions? Commented Jul 28, 2011 at 22:55
  • 1
    No but you don't have to next functionTwo inside of the anonymous function. -- $('.div').animate({...},functionTwo); Commented Jul 28, 2011 at 22:56
  • I have a script that is quite large and I have functions being re-used by various events and methods. It's getting a little messy Commented Jul 28, 2011 at 23:00

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.