Created
July 18, 2012 19:57
-
-
Save wildlyinaccurate/3138488 to your computer and use it in GitHub Desktop.
Really simple Javascript queueing system
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
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