Created
November 25, 2011 14:07
-
-
Save craigrodway/1393600 to your computer and use it in GitHub Desktop.
Javascript function queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* In the <head> | |
*/ | |
var jsq = (function(){ | |
var q = []; // internal queue of functions that are added | |
var fs = {}; // public exportable functions | |
// Add a function to the queue | |
fs.add = function(f){ | |
q.push(f); | |
}; | |
// Run the queued functions | |
fs.run = function(){ | |
if (typeof(q) != "undefined") { | |
for (var i=0, len=q.length; i<len; i++) { | |
q[i](); | |
} | |
} | |
}; | |
return fs; | |
})(); | |
/** | |
* In the middle of the page, anywhere: | |
* | |
* <script>jsq.add(function(){ | |
* // Initialise some jQuery thing | |
* $("div.example").doSomething(); | |
* })</script> | |
* | |
* | |
* At the foot of the page: | |
* | |
* <script src="js/jquery-1.7.1.min.js"></script> | |
* <script type="text/javascript"> | |
* $(document).ready(function(){ jsq.run(); }); | |
* </script> | |
* | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment