Synchronize jquery animations in callback
I have a runner
function that should only return when all stuff (also animations) in the called callback are finished.
function runner(callback) {
callback.call();
return; // only return when callback is finished开发者_StackOverflow with everything.
}
I thought about using the jQuery queue
function, but I could not get it working. Is it even possible to archive that. callback
is a blackbox. runner
is not able to know whats going up in there.
What you really want is for callback
to take a callback of its own. (I know you said it's a black box, but it needs to be, well, a different black box.) Then instead of waiting for the callback to return, you just pass the continuation to it and callback
is responsible for assigning that continuation to the completion handler on the animation (or whatever it does). So instead of:
doSomeStuff();
runner(callback);
doMoreStuff();
you want
doSomeStuff();
callback.call(doMoreStuff);
Here's an answer to a related question. More generally, this pattern is continuation-passing style and is central to how jQuery handles things.
精彩评论