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!
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);
}
All defer
does is execute the function inside window.setTimeout
with timeout of 0.
You can implement it like this I am sure:
Function.prototype.defer = function() {
var __method = this, args = Array.prototype.slice.call(arguments, 1);
return window.setTimeout(function() {
return __method.apply(__method, args);
}, 0);
}