views:

85

answers:

3

Is there a jQuery equivalent to prototype's defer?

I'm looking for something that will delay the execution of a script until all of the scripts in the page are finished executing.

Thanks!


PART II: Is there any way to see if there are other setTimeouts in the queue and delay execution until after they fire? I see in the comments that sometimes setTimeout of 0 or 1 doesn't matter because it's unpredictable as to which will fire first.

Thanks again!

Update to answer

I found a bug in the code that I was using from the answer accepted below. The slice call needs to work on 0, not 1 since in the Prototype core code, it's accepting an extra parameter for the amount of time to wait (0.01). The final method then becomes:

Function.prototype.deferFunc = function() {
   var __method = this, args = Array.prototype.slice.call(arguments, 0);
   return window.setTimeout(function() {
      return __method.apply(__method, args);
   }, 0.01);
}
+5  A: 

You can use the basic version available in vanilla JavaScript for most purposes, setTimeout():

setTimeout(function() {
  //do something
}, 0);

A similar queuing mechanism jQuery uses for animations are the callbacks and the .delay() function (which uses setTimeout() underneath).

Nick Craver
+1 for vanilla JS. The only difference is that *defer()* can accept variables as arguments that will be stored in their current state, kind of like a combination of *setTimeout()* and *bind()*.
Andy E
+2  A: 

All defer does is execute the function inside window.setTimeout with timeout of 0.

You can implement it like this I am sure:

Function.defer = function() {
        var __method = this, args = slice.call(arguments, 1);
        return window.setTimeout(function() {
          return __method.apply(__method, args);
        }, 0);
      }

Demo

Strelok
It should be `Function.prototype.defer`. Not to mention `slice.call` should be `Array.prototype.slice.call`.
Andy E
I decided to go with this implementation since it can accept methods with parameters. It also seems closest to the implementation used by prototype (the library). THANK YOU VERY MUCH!!!!
shmuel613
BTW, I also want to verify that you DO need the changes that Andy E suggests above!
shmuel613
A: 

you can easily implement it:

Function.prototype.defer=function() {
    setTimeout(this, 0);
}

and here's a test:

var testFunc=function() {
    alert('first');
}
testFunc.defer();
alert('second');//first the browser will run this line, then will execute the above defered one.
aularon
Setting the timer to 1 won't add the callback to the thread queue until 1ms later, setting the timer to 0 will add it to the thread queue immediately.
Andy E
Not correct Andy E, all browsers have a varying minimum timeout restriction and furthermore, the timeout will never be exact as it will wait for any script thats executing untill there's an opening.
BGerrissen
@BGerrissen - @Andy is correct, for example try this in IE: http://jsfiddle.net/nick_craver/QJdEg/ Notice the `setTimeout(func, 1)` gets queued *after* the `setTimeout(func, 0)`, so setting it to 1 isn't the same, as a timeout of 0 can still jump in front, depending on the browser.
Nick Craver
Thanks, I fixed that.
aularon
Prototype actually uses 10ms, not 0, internally. Their docs are a touch misleading.
Crescent Fresh