Is there a jQuery equivalent for a prototype?

Is there a jQuery equivalent for a prototype?

I am looking for something that will delay script execution until all scripts on the page are executed.

Thanks!


PART II: Is there a way to see if there are other setTimeouts queues in the queue and defer execution until they light up? I see in the comments that sometimes setTimeout 0 or 1 does not matter, because it is unpredictable as to what will fire first.

Thanks again!

Refresh for reply

I found an error in the code that I used from the answer below. The slice call should work on 0, not 1, because in the Prototype kernel code it takes an additional parameter for the timeout (0.01). The final method will then be:

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); } 
+6
javascript jquery prototypejs
source share
3 answers

All defer executes a function inside window.setTimeout with a timeout of 0.

You can implement it like this, I'm 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); } 

Demo

+4
source share

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

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

A similar queue mechanism used by jQuery for animations is callbacks and the .delay() function (which uses setTimeout() below).

+9
source share

you can easily implement it:

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

and here is the test:

 var testFunc=function() { alert('first'); } testFunc.defer(); alert('second');//first the browser will run this line, then will execute the above defered one. 
+1
source share

All Articles