JSFiddle - React, Tailwind, and code Playground
by darcyclarke
HTML
<script src="https://raw.github.com/mennovanslooten/mockJSON/master/js/jquery.mockjson.js"></script>
JavaScript
var App = {};
App.queue = new Array;
App.remove = function(arr, el){
for(var i=0;i<arr.length;i++){
if(arr[i]===el){
arr.splice(i,1);
return true;
}
}
return false;
};
App.ajax = function(options){
options = jQuery.extend({ dequeue: function(){ App.ajaxDequeue(this.id); }, id: ((new Date()).getTime() + "" + Math.floor(Math.random() * 1000000)).substr(0, 18), action : false, queue : false}, options);
App.queue[options.id] = options;
if( options.action !== false ){
if( App.queue[options.action] === false || App.queue[options.action] === undefined || options.queue === false ){
if( options.queue === false && App.queue[options.action][0].abort ){
App.queue[options.action][0].always(function(){});
App.queue[options.action][0].abort();
}
App.queue[options.action] = [];
}
App.queue[options.action].push(options.id);
if( App.queue[options.action].length === 1 ){
App.ajaxRun(options.action);
return App.queue[options.id];
}
return App.queue[options.id];
} else {
App.queue[options.id] = jQuery.ajax(options);
return App.queue[options.id];
}
};
App.ajaxRun = function(action){
jqXHR = App.queue[App.queue[action][0]] = jQuery.ajax(App.queue[App.queue[action][0]]);
jqXHR.always(function(){
App.remove(App.queue,App.queue[App.queue[action][0]]);
App.queue[action].splice(0,1);
if( App.queue[action].length >= 1 ){
App.ajaxRun(action);
}
});
jqXHR.dequeue = function(){ App.ajaxDequeue(App.queue[action][0]); };
return jqXHR;
};
App.ajaxDequeue = function(id){
App.remove(App.queue, App.queue[id]);
if(App.queue[id].abort){
App.queue[id].abort();
}
if(App.queue[id].action !== false){
App.remove(App.queue[App.queue[id].action],id);
...