Skip to content

Instantly share code, notes, and snippets.

@craigrodway
Created November 25, 2011 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigrodway/1393600 to your computer and use it in GitHub Desktop.
Save craigrodway/1393600 to your computer and use it in GitHub Desktop.
Javascript function queue
/**
* 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