Skip to content

Instantly share code, notes, and snippets.

@jonathonbyrdziak
Created April 14, 2012 05:54
Show Gist options
  • Save jonathonbyrdziak/2382331 to your computer and use it in GitHub Desktop.
Save jonathonbyrdziak/2382331 to your computer and use it in GitHub Desktop.
jquery AJAX QUEUE : Quick Ajax Queue Manager
/**
* 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);
}
});
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)
});
@emgiezet
Copy link

Nice work! I just looking for sth like this. It will be nice to modify the script, that the queue will be triggered just once. Without the setTimeout. It can be made in some parameter like listener mode or sth.

@nodesocket
Copy link

Note, that Simple JavaScript Inheritance code by John Resig is required for using Class.extend():

/* Simple JavaScript Inheritance
 * By John Resig http://ejohn.org/
 * MIT Licensed.
 */
// Inspired by base2 and Prototype
(function() {
  var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
  this.Class = function(){};

  Class.extend = function(prop) {
    var _super = this.prototype;
    initializing = true;
    var prototype = new this();
    initializing = false;

    for (var name in prop) {
      prototype[name] = typeof prop[name] == "function" && 
        typeof _super[name] == "function" && fnTest.test(prop[name]) ?
        (function(name, fn){
          return function() {
            var tmp = this._super;

            this._super = _super[name];

            var ret = fn.apply(this, arguments);        
            this._super = tmp;

            return ret;
          };
        })(name, prop[name]) :
        prop[name];
    }

    function Class() {
      if ( !initializing && this.init )
        this.init.apply(this, arguments);
    }

    Class.prototype = prototype;

    Class.prototype.constructor = Class;

    Class.extend = arguments.callee;

    return Class;
  };
})();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment