views:

2688

answers:

3

Basically I want to be able to wrap any command in a $.holdTillFinished() method that will not allow execution to continue until the specified method/jquery animation is finished executing.

I suspect that this may be difficult or even impossible in javascript, but I'm hoping someone will know.

I don't want to hear about how I can pass a callback into an animation to have it run oncomplete, or anything else like that.

I really want to know if such a thing is possible and how to go about it.

If that isn't going to happen if anyone knows of a nice queue plugin that is capable of queuing both user defined methods and animations, that would be cool too. What I really want is a way to delay execution though.(while still allowing animations to function)

+2  A: 

You're probably looking for http://docs.jquery.com/Effects/queue

But if you're looking for a more generalized way of delaying things, I've used this snippet before (I didn't write it, so all credit goes to the original author):

//Simple wrapper enabling setTimout within chained functions.
$.fn.wait = function(time, type) {
  time = time || 1000;
  type = type || "fx";
  return this.queue(type, function() {
    var self = this;
    setTimeout(function() {
      $(self).dequeue();
    }, time);
  });
};
Flemish Bee Cycle
This doesn't delay execution until the function ends.What this does is delay execution in the queue by some time limit that I am setting. I don't know how long it takes to run any given function and I shouldn't have to guess.
thirsty93
This is better then nothing though. But it's really not what I'm looking for.
thirsty93
+2  A: 

I suspect that this may be difficult or even impossible in javascript

Your suspicion is correct. Methods like animations, user input loops and ‘async=true’ XMLHttpRequests must return control to the browser in order to proceed, and the browser can't get back control until every nesting level of function call has returned. That means all your code, including the function that called ‘holdTillFinished()’ would have to unwind: therefore ‘holdTillFinished()’ is impossible.

Other languages have flow-control features that allow you to effectively execute an asynchronous process as one that appears to the caller to be synchronous, and similarly vice-versa. The best-known are threads and continuations. JavaScript does not possess these facilities, so the best you can do is timeouts and callbacks.

(Defining a callback as an inline function to gain access to the enclosing function's variables in a closure does at least take some of the pain out of it; some other languages have to start wrapping every bit of enclosing state in an object's properties to achieve this.)

bobince
What are these said other languages? Curious...
Crescent Fresh
Well pretty much all modern (non-browser) languages give you threads, using OS support. Continuations/coroutines can be found in at least Haskell, ML and Scheme and some extensions to other languages (eg. Stackless Python); there are primitives in C that can be used to build continuations.
bobince
Of course, in the browser world, you're stuck: apart from JavaScript there's only VBScript, and that's even worse! JavaScript 1.7 (in Firefox 2) gives you Python-style generators, which are a limited form of continuation that wouldn't really help you here.
bobince
Shoot, I actually meant "what languages have to start wrapping every bit of enclosing state in an object's properties to achieve this?".
Crescent Fresh
Languages without first-class functions and closures, such as C[++] and Java. (Java's anonymous inline classes can almost do it, in that they can read but not write outer function variables.)
bobince
A: 

You could use a publish/subscribe architecture to achieve what you want. Without knowing much about what exactly you want to achieve, my guess is this will work for you.

The way this works is that you look at the completion of function as an event, which you can listen to and attach handlers to. Dojo has a pubsub mechanism, though I'm sure there are others.

Rakesh Pai
This is exactly what I do not want. A callback that I pass in to the function/animation in question
thirsty93