JQuery Ajaxqueue Example Detailed

Source: Internet
Author: User

The complete callback can be an array of functions after jquery1.5, called in array order.
If the previous request is not returned and the new request is issued, then the previous request is revoked, which is the new request "overwrite" the original request.
As an object-oriented form, and then with a ajaxmanage for simple management.

The code is as follows Copy Code

;(function ($) {
Override: Does the new request overwrite the previous request
function Ajaxqueue (override) {
This.override =!! Override
};

Ajaxqueue.prototype = {

Requests:new Array (),

Offer:function (options) {
var _self = this;

var xhroptions = $.extend ({}, options, {
Complete:function (JQXHR, Textstatus) {
Support for complete is a function array case
if ($.isarray (Options.complete)) {

var funcs = Options.complete;
for (var i = 0, len = funcs.length i < len; i++)
Funcs[i].call (This, jqxhr, textstatus);

} else {

if (Options.complete)
Options.complete.call (This, jqxhr, textstatus);
}

Processing ended, issuing the next AJAX request from the queue
_self.poll ();
},

Beforesend:function (JQXHR, settings) {
if (options.beforesend)
var ret = Options.beforeSend.call (this, jqxhr, settings);
If the current AJAX request is revoked for some reason, then send the next Ajax request
if (ret = false) {
_self.poll ();
return ret;
}
}
});

If overlay is supported, then call replace
if (this.override) {
Console.log (' Go override ');
This.replace (xhroptions);
Instead put in the queue
} else {
Console.log (' Go queue ');
This.requests.push (xhroptions);

if (this.requests.length = = 1) {
$.ajax (xhroptions);
}
}
},

Undo the previous request and send a new request
Replace:function (xhroptions) {
var Prevret = This.peek ();
if (Prevret!= null) {
This method can be seen in the jquery source code
Prevret.abort ();
}

This.requests.shift ();
This.requests.push ($.ajax (xhroptions));
},

Poll queue send next request
Poll:function () {
if (This.isempty ()) {
return null;
}

var processedrequest = This.requests.shift ();
var nextrequest = This.peek ();
if (nextrequest!= null) {
$.ajax (nextrequest);
}

return processedrequest;
},

Return request to queue header
Peek:function () {
if (This.isempty ()) {
return null;
}

var nextrequest = this.requests[0];
return nextrequest;
},

To determine whether a queue is empty
Isempty:function () {
return this.requests.length = = 0;
}
};

var queue = {};

Simple objects to manage ajaxqueue
var Ajaxmanager = {
Create a new Ajaxqueue
Createqueue:function (name, override) {
return Queue[name] = new Ajaxqueue (override);
},

Clear Ajaxqueue corresponding to name
Destroyqueue:function (name) {
if (Queue[name]) {
Queue[name] = null;
Delete Queue[name];
}
},

Get the corresponding Ajaxqueue according to name
Getqueue:function (name) {
Return (Queue[name]? Queue[name]: null);
}
};

Associated with jquery, for short, easy to invoke
$.am = Ajaxmanager;
}) (JQuery);


In fact, also want to add done,fail,always and other configuration, but may become a bit more complicated, just keep it simple

Here are two of my jsfiddle pages, one is the overlay effect, one is the queue effect, you can test the run directly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.