Forcing sequential custom functions with jQuery

Is it at all possible to force synchronous and sequential calls to my custom javascript functions with jQuery?

doThis();
doThat();

function doThis() {
// This takes a while, but we only want to do doThat() once doThis() is finished
}

function doThat() {
// This takes much less time, but we only want it to fire off when doThis() is done.
}

With jQuery animation, there’s a nice callback functionality where the animation waits until completion before the callback is called. But is there a way I can do similar for custom functions?

Yes there is.


function doThis(callback) {
    // This takes a while, but we only want to do doThat() once doThis() is finished

    // now fire off the callback
    if (typeof callback === 'function') {
        callback();
    }
}

function doThat() {
    // This takes much less time, but we only want it to fire off when doThis() is done.
}

doThis(doThat);

Paul.

Wow, very neat. It’s not jQuery (which, I admit, would have been the preferred method of invocation), but this will more than suffice for what I need. Thank you very much for your help!