Skip to content

Instantly share code, notes, and snippets.

@extralam
Forked from guybedford/jquery.ajaxqueue.js
Last active December 17, 2020 10:33
Show Gist options
  • Save extralam/ef68e6d9bd83a68b82b3 to your computer and use it in GitHub Desktop.
Save extralam/ef68e6d9bd83a68b82b3 to your computer and use it in GitHub Desktop.

Revisions

  1. extralam revised this gist Oct 14, 2015. 1 changed file with 73 additions and 0 deletions.
    73 changes: 73 additions & 0 deletions jquery.ajaxqueue2.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    /*
    Allows for ajax requests to be run synchronously in a queue
    with pool , without cancel
    Usage::
    var queue = new $.AjaxQueue();
    // add with tag name or not , both ok
    queue.add('test',{
    url: 'url',
    complete: function() {
    console.log('ajax completed');
    },
    _run: function(req) {
    //special pre-processor to alter the request just before it is finally executed in the queue
    req.url = 'changed_url'
    }
    });
    */

    $.AjaxQueue = function() {
    this.MAX_POOL_SIZE = 3;
    this.CURR_POOL_SIZE = 0;
    this.reqs = [];
    };

    $.AjaxQueue.prototype = {
    add: function(req) {
    //console.log("Add Request : ");
    this.reqs.push({n : '' , r: req });
    this.next();
    },
    add: function(name , req) {
    console.log("Add Request : " + name);
    this.reqs.push( {n : name , r: req });
    this.next();
    },
    remove: function (name){
    // TODO: NOT IMPLEMENT
    },
    next: function() {
    console.log(' Q Size ' + this.reqs.length);
    if (this.reqs.length == 0) {
    return;
    }

    if(this.CURR_POOL_SIZE >= this.MAX_POOL_SIZE){
    return;
    }

    var request = this.reqs.splice(0, 1)[0];
    var req = request['r'];

    var complete = req.complete;
    var self = this;

    self.current_reqs_tag = request['n'];

    if (req._run) {
    req._run(req);
    }
    req.complete = function() {
    if (complete) {
    complete.apply(this, arguments);
    }
    self.CURR_POOL_SIZE--;
    self.next();
    }

    self.CURR_POOL_SIZE++;
    self.current_req = $.ajax(req);
    }
    };
  2. extralam revised this gist Oct 13, 2015. 1 changed file with 81 additions and 43 deletions.
    124 changes: 81 additions & 43 deletions jquery.ajaxqueue.js
    Original file line number Diff line number Diff line change
    @@ -1,53 +1,91 @@
    /*
    :: Enhanced Version @extralam ::
    Allows for ajax requests to be run synchronously in a queue , remove queue
    Usage::
    var queue = new $.AjaxQueue();
    Allows for ajax requests to be run synchronously in a queue
    // add with tag name or not , both ok
    queue.add('test',{
    url: 'url',
    complete: function() {
    console.log('ajax completed');
    },
    _run: function(req) {
    //special pre-processor to alter the request just before it is finally executed in the queue
    req.url = 'changed_url'
    }
    });
    Usage::
    // Cancel Request
    queue.remove('test');
    var queue = new $.AjaxQueue();
    queue.add({
    url: 'url',
    complete: function() {
    console.log('ajax completed');
    },
    _run: function(req) {
    //special pre-processor to alter the request just before it is finally executed in the queue
    req.url = 'changed_url'
    }
    });
    */
    */

    $.AjaxQueue = function() {
    this.reqs = [];
    this.requesting = false;
    this.reqs = [];
    this.requesting = false;
    this.current_reqs_tag = null;
    this.current_req = null;
    };

    $.AjaxQueue.prototype = {
    add: function(req) {
    this.reqs.push(req);
    this.next();
    },
    next: function() {
    if (this.reqs.length == 0)
    return;

    if (this.requesting == true)
    return;

    var req = this.reqs.splice(0, 1)[0];
    var complete = req.complete;
    var self = this;
    if (req._run)
    req._run(req);
    req.complete = function() {
    if (complete)
    complete.apply(this, arguments);
    self.requesting = false;
    self.next();
    }
    add: function(req) {
    //console.log("Add Request : ");
    this.reqs.push({n : '' , r: req });
    this.next();
    },
    add: function(name , req) {
    console.log("Add Request : " + name);
    this.reqs.push( {n : name , r: req });
    this.next();
    },
    remove: function (name){
    if(this.current_reqs_tag == name){
    console.log("Cancel Request : " + name);
    this.current_req.abort();
    this.current_req = null;
    this.current_reqs_tag = null;
    this.requesting = false;
    this.next();
    }else{
    for (var i = 0; i < this.reqs.length; i++) {
    if(this.reqs[i]['n'] == name){
    this.reqs.splice(i, 1);
    break;
    }
    }
    }
    },
    next: function() {
    console.log(' Q Size ' + this.reqs.length);
    if (this.reqs.length == 0) {
    return;
    }

    if (this.requesting == true) {
    return;
    }
    var request = this.reqs.splice(0, 1)[0];
    var req = request['r'];

    this.requesting = true;
    $.ajax(req);
    }
    var complete = req.complete;
    var self = this;

    self.current_reqs_tag = request['n'];

    if (req._run) {
    req._run(req);
    }
    req.complete = function() {
    if (complete) {
    complete.apply(this, arguments);
    }
    self.requesting = false;
    self.next();
    }

    self.requesting = true;
    self.current_req = $.ajax(req);
    }
    };
  3. @guybedford guybedford revised this gist Apr 23, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions jquery.ajaxqueue.js
    Original file line number Diff line number Diff line change
    @@ -38,14 +38,14 @@ $.AjaxQueue.prototype = {
    var req = this.reqs.splice(0, 1)[0];
    var complete = req.complete;
    var self = this;
    if (req._run)
    req._run(req);
    req.complete = function() {
    if (complete)
    complete.apply(this, arguments);
    self.requesting = false;
    self.next();
    }
    if (req._run)
    req._run(req);

    this.requesting = true;
    $.ajax(req);
  4. @guybedford guybedford revised this gist Apr 23, 2012. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion jquery.ajaxqueue.js
    Original file line number Diff line number Diff line change
    @@ -39,7 +39,8 @@ $.AjaxQueue.prototype = {
    var complete = req.complete;
    var self = this;
    req.complete = function() {
    complete.apply(this, arguments);
    if (complete)
    complete.apply(this, arguments);
    self.requesting = false;
    self.next();
    }
  5. @guybedford guybedford revised this gist Apr 23, 2012. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions jquery.ajaxqueue.js
    Original file line number Diff line number Diff line change
    @@ -43,6 +43,8 @@ $.AjaxQueue.prototype = {
    self.requesting = false;
    self.next();
    }
    if (req._run)
    req._run(req);

    this.requesting = true;
    $.ajax(req);
  6. @guybedford guybedford created this gist Apr 23, 2012.
    50 changes: 50 additions & 0 deletions jquery.ajaxqueue.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    /*
    Allows for ajax requests to be run synchronously in a queue
    Usage::
    var queue = new $.AjaxQueue();
    queue.add({
    url: 'url',
    complete: function() {
    console.log('ajax completed');
    },
    _run: function(req) {
    //special pre-processor to alter the request just before it is finally executed in the queue
    req.url = 'changed_url'
    }
    });
    */

    $.AjaxQueue = function() {
    this.reqs = [];
    this.requesting = false;
    };
    $.AjaxQueue.prototype = {
    add: function(req) {
    this.reqs.push(req);
    this.next();
    },
    next: function() {
    if (this.reqs.length == 0)
    return;

    if (this.requesting == true)
    return;

    var req = this.reqs.splice(0, 1)[0];
    var complete = req.complete;
    var self = this;
    req.complete = function() {
    complete.apply(this, arguments);
    self.requesting = false;
    self.next();
    }

    this.requesting = true;
    $.ajax(req);
    }
    };