Skip to content

Instantly share code, notes, and snippets.

@imana97
Last active August 29, 2015 14:00
Show Gist options
  • Save imana97/11253567 to your computer and use it in GitHub Desktop.
Save imana97/11253567 to your computer and use it in GitHub Desktop.
queueing function execution in javascript
var Queue = function() {
// copy this to self
var self = this;
//initiating an empty queue
self.queue = [];
//initiating an empty object for to keep the last json data fetched by ajax
self.lastData = null;
// this methods add a function to the queue
self.next = function(func) {
//inserting the function as value to the queue
self.queue.push(func);
//returning this to enable chaining
return self;
};
// this methods run the next function in the queue
self.callback = function() {
// removing the first function in the queue. because it is already executed
self.queue.shift();
// if there are still more function to execute, execute the first function.
//this is recursive function
if (self.queue.length > 0) {
_runArray(self.queue[0]);
}
}
// this function turns a function to string and identifies its parameters dynamically
var _getParamNames = function(func) {
var funStr = func.toString();
var arr = funStr.slice(funStr.indexOf('(') + 1, funStr.indexOf(')')).match(/([^s,]+)/g);
// return true if the function has no parameter
return (arr == null);
}
// this function execute a function in and pass self.callback as its parameter.
// this one is a bit complex. need a lot of thinking :)
var _runArray = function(func) {
//if has no callback parameter, then it is a plugin.
//if there is callback parameter, then it is manually writting after object creating.
_getParamNames(func) ? func() : func(self.callback);
}
// this method run the queuse from 0..N
self.run = function() {
if (self.queue.length > 0) {
_runArray(self.queue[0]);
}
};
};
//HOW TO USE
var q=new Queue();
// first function
q.next(function(callback){
// your code here
//when you want the next function to execute
callback();
});
// Nth function
q.next(function(callback){
// nth function
//
//the callback is not really neccessary for the last function,but you can call it
callback();
});
// run the queue
q.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment