Automatically give up:
When initiating an AJAX request, if the previous request is not completed, it will be automatically abandoned.
Modify the parameter isAbort = false to turn off this item
AJAX request queue:
Execute a group of AJAX requests in sequence . After the previous request is successful, execute the next one
/** * ajax communication class * Depends on jQuery 1.6+ * Extend jQuery's ajax object to implement: * 1. Inherit the parameters of the last request, * 2. If the last request is not completed, it will be automatically abandoned. * 3. Ajax queue requirements * * by oTwo [email protected] * v 1.0 July 31, 2013 * * illustrate: * var db = new oTwo.DB(option) option: ajax general settings See comments for details * db.xhr jqXHR is a superset of the browser’s XMLHttpRequest object * db.queueList array request queue (if saved) * db.defaultAjaxOption Object ajax default settings * * .get(option,[isExtend],[isAbort]) is used to obtain GET requests. * .queue(optionArr,[flag]) ajax queue request * .setDefault(option) sets the default ajax request parameters * .send(option) inherits the default settings and sends the request. */ var oTwo = oTwo || {}; oTwo.DB = function(option) { this.xhr = {}; this.queueList = []; this.defaultAjaxOption = { url: "", type: "GET", data: "", dataType: "json", // Timeout (milliseconds) After the timeout, .fail() will be executed, and .done() will not be executed. timeout: "", //async:false synchronous request async: true, // cache:false does not cache this page, true caches this page cache: false, //Force HTTP header to solve Chinese garbled characters contentType: 'application/x-www-form-urlencoded; charset=utf-8' } option = $.extend(true,this.defaultAjaxOption, option); this.setDefault(option); this.ajaxOption =$.extend(true,{},this.defaultAjaxOption); } oTwo.DB.prototype = { /** * Used to obtain GET requests. Related settings can be modified through option * .get() repeats the last instruction * * .get(option,[isExtend],[isAbort]) new request * @param Object option ajax setting parameters *----Please refer to http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings * @param boolean isExtend inherits the parameters of the last request * ---- Default is true, if it is false, this function is turned off. * @param boolean isAbort When initiating an AJAX request, it will automatically give up if the previous request is not completed. * ---- Default is true, if it is false, this function is turned off. * * @return The return object of jqXHR $.ajax(), a superset of the browser's XMLHttpRequest object. *----its common methods * ----jqXHR.done(function(data, textStatus, jqXHR) {}); Request successful * ----jqXHR.fail(function(jqXHR, textStatus, errorThrown) {}); Request failed * ----jqXHR.always(function(data|jqXHR, textStatus, jqXHR|errorThrown) { });Request completed * ---Please refer to http://api.jquery.com/jQuery.ajax/#jqXHR */ get: function(option, isExtend, isAbort) { if (typeof option === "string") { option = { "url": option } }; //Abandon the last unfinished request if (this.xhr && this.xhr.done && isAbort !== false) { this.xhr.abort(); }; // ajaxOption returns to default settings if (isExtend === false) { this.setDefault(); }; this.ajaxOption = $.extend(true,this.ajaxOption, option); this.xhr = this.send(this.ajaxOption); return this.xhr; }, /** * Please ignore it for internal methods * Inherit the default settings and send the request. * @param Object option ajax setting parameters * @return jqXHR */ send: function(option) { option = $.extend(true, {},this.defaultAjaxOption, option); var xhr = $.ajax(option); return xhr; }, /** * .setDefault(option) *Set default ajax request parameters * @param Object option ajax setting parameters * @return oTwo.DB * * .setDefault() *Restore to default settings */ setDefault: function(option) { if (!option) { this.ajaxOption = $.extend({}, this.defaultAjaxOption); return this; }; this.defaultAjaxOption = $.extend(true,{}, option); return this; }, /** * ajax queue request * .queue() * Get the current queue * * .queue(optionArr,[flag]) * @param array option request array * ---- $.extend(ajaxoption, {callback:function(data){return true}}); * ---- ajaxoption: ajax setting parameters will be inherited separately * ---- callback: The callback function after the request is successful. If callback returns false, subsequent requests will be abandoned. * ---- If the request is unsuccessful, subsequent requests will also be abandoned. * * @param boolean isSaveQueue whether to save the request queue * ---- Default true (save) */ queue: function(queueList, isSaveQueue) { if (!queueList) { return; }; if (isSaveQueue === false) { this.queueList = queueList; }; if (queueList.length === 0) { return; }; var ss = queueList.shift(), self = this; this.send(ss.ajaxoption).done(function(d) { var isGoOn = ss.callback(d); if (isGoOn !== false) { self.queue(queueList, false); }; }); } }