Created
April 14, 2012 05:54
-
-
Save jonathonbyrdziak/2382331 to your computer and use it in GitHub Desktop.
jquery AJAX QUEUE : Quick Ajax Queue Manager
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
/** | |
* Quick Ajax Queue Manager | |
* | |
* Inspired by jAndy at Stackoverflow | |
* http://stackoverflow.com/questions/4785724/queue-ajax-requests-using-jquery-queue | |
* | |
*/ | |
var AjaxQ = Class.extend | |
({ | |
// Default options | |
defaults: { | |
requests : [] | |
}, | |
// Initializing | |
init: function(options) | |
{ | |
// initializing variables | |
this.o = jQuery.extend({},this.defaults,options); | |
this.run(); | |
}, | |
// Method is constantly searching for ajax requests | |
run: function() | |
{ | |
if( this.o.requests.length ) | |
{ | |
this.stall = this.o.requests[0].complete; | |
this.o.requests[0].complete = this.complete.bind(this); | |
jQuery.ajax(this.o.requests[0]); | |
} | |
else | |
{ | |
setTimeout(function() { | |
this.run.apply(this, []); | |
}.bind(this), 50); | |
} | |
}, | |
complete: function() | |
{ | |
if( typeof this.stall === 'function' ) this.stall(); | |
this.o.requests.shift(); | |
this.run.apply(this, []); | |
}, | |
queue: function(opt) | |
{ | |
this.add(opt); | |
}, | |
add: function(opt) | |
{ | |
this.o.requests.push(opt); | |
return this; | |
}, | |
remove: function(opt) | |
{ | |
if( jQuery.inArray(opt, this.o.requests) > -1 ) | |
this.o.requests.splice(jQuery.inArray(opt, this.o.requests), 1); | |
return this; | |
}, | |
stop: function() | |
{ | |
this.o.requests = []; | |
clearTimeout(this.tid); | |
} | |
}); |
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 a = new AjaxQ(); | |
a.queue({ | |
url : this.o.url, | |
data : this.inputs, | |
beforeSend : this.beforeSend.bind(this), | |
success : this.success.bind(this) | |
}); | |
a.queue({ | |
url : this.o.url, | |
data : this.inputs, | |
//beforeSend : this.beforeSend.bind(this), | |
success : this.success1.bind(this) | |
}); | |
a.queue({ | |
url : this.o.url, | |
data : this.inputs, | |
//beforeSend : this.beforeSend.bind(this), | |
success : this.success2.bind(this) | |
}); | |
a.queue({ | |
url : this.o.url, | |
data : this.inputs, | |
success : this.success3.bind(this) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note, that
Simple JavaScript Inheritance
code by John Resig is required for usingClass.extend()
: