Skip to content

Instantly share code, notes, and snippets.

@wildlyinaccurate
Created July 18, 2012 19:57
Show Gist options
  • Save wildlyinaccurate/3138488 to your computer and use it in GitHub Desktop.
Save wildlyinaccurate/3138488 to your computer and use it in GitHub Desktop.
Really simple Javascript queueing system
var Queue = function() {
var isInit = false;
var queue = [];
return {
onInit: function(callback) {
if (isInit) {
callback();
} else {
queue.push(callback);
}
},
init: function() {
while (queue.length) {
(queue.shift())();
}
isInit = true;
}
};
}();
// Basic usage
Queue.onInit(function() {
// App might not be defined at this point
App.doSomething();
});
Queue.onInit(function() {
App.doSomethingElse();
});
// Later on, after App has been initialised, the items are processed in the order that they were queued.
// App.doSomething() is run, and then App.doSomethingElse()
Queue.init();
Queue.onInit(function() {
// Any items added after the queue has already been processed will be run straight away
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment