Skip to content

Instantly share code, notes, and snippets.

@rendfall
Created March 6, 2015 13:18
Show Gist options
  • Save rendfall/dc737bcfc16f7a563ee5 to your computer and use it in GitHub Desktop.
Save rendfall/dc737bcfc16f7a563ee5 to your computer and use it in GitHub Desktop.
/**
* Queue System Object
*
* DOM structure requires some time to load,
* so we need to wait with events attachment.
* Queuing allows to manipulate functions call order
* properly.
*
* Important:
* Remember to add Queue.done() in the very end
* of every queued functions to tell it was finished.
*
* Of course, we can have multiple queues binded by name
* but in this case we used just one.
*
* To make life easier we can pass one function
* or array of functions.
*/
var Queue = {
name: 'myQueueName',
attachedTo: $('body'),
add: function(fnArr){
fnArr = (fnArr.constructor === Array) ? fnArr : [fnArr];
var el = this.attachedTo;
var name = this.name;
for(var idx in fnArr){
var fn = fnArr[idx];
if(typeof fn === 'function') {
el.queue(name, fn);
}
}
},
done: function(){
this.attachedTo.dequeue(this.name);
}
}
var myFunctionOne = function() { alert('one'); Queue.done(); }
var myFunctionTwo = function() { alert('two'); Queue.done(); }
// For example
Queue.add([
myFunctionOne,
myFunctionTwo,
function(){
alert('three');
}
]);
// Let's start Deus Ex Machina
Queue.done();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment