real schrieb:
Not quite, I don't necessarily want a delay in a set amount of time, I
was looking to execute the second function after the first one has
completed. For animations and AJAX calls, jQuery already handles that,
but I wanted to integrate a callback functionality in my own custom
methods.
The delay is just one example of using callbacks. The basic principle is always the same: Pass in a function reference and call that later.

In case you are looking for a way to queue callbacks and automatically call one after the other: jQuery' testsuite[1] contains some code that could give you a good piece to start. I wrote that queuing stuff, let me know if you need any help with that.

The intersting methods are synchronize() and process(). stop() and start() is important to run asynchronous tests.

var _config = {
        queue: [],
        blocking: true,
        timeout: null,
        asyncTimeout: 2 // seconds for async timeout
};

function synchronize(callback) {
        _config.queue[_config.queue.length] = callback;
        if(!_config.blocking) {
                process();
        }
}

function process() {
        while(_config.queue.length && !_config.blocking) {
                var call = _config.queue[0];
                _config.queue = _config.queue.slice(1);
                call();
        }
}

function stop(allowFailure) {
        _config.blocking = true;
        var handler = allowFailure ? start : function() {
                ok( false, "Test timed out" );
                start();
        };
        _config.timeout = setTimeout(handler, _config.asyncTimeout * 1000);
}
function start() {
        if(_config.timeout)
                clearTimeout(_config.timeout);
        _config.blocking = false;
        process();
}

You add callbacks by passing them to synchronize().

[1] http://dev.jquery.com/browser/trunk/jquery/build/test/data/testrunner.js

--
Jörn Zaefferer

http://bassistance.de

Reply via email to