JSFiddle - React, Tailwind, and code Playground

by bgrins

JavaScript

(function($) {

    var queue = {};

    $.ajaxq = function(name, opts) {
    
        var opts = $.extend(true, {}, opts);
        
        enqueue(function() {
            $.ajax.apply(window, [opts]).always(dequeue);
        });
        
        function enqueue(cb) {
            console.log(typeof queue[name], queue[name], $.isArray(queue[name]));
            
            // If there is no queue, create an empty one and instantly process this item.  
            if (typeof queue[name] === "undefined") {
                queue[name] = [];
                cb();
            }
            // Otherwise, just add this item onto it.
            else {
                queue[name].push(cb);
            }
        }
        
        function dequeue() {
            
            var isLastCallback = queue[name].length === 1;
            var nextCallback = queue[name].shift();
            
            // Fire off the next callback (will not exist if the queue was empty)
            if (nextCallback) {
                nextCallback();
            }
            
            // Remove the queue so the next one will automatically fire.
            if (isLastCallback && queue[name].length === 0) {
                delete queue[name];   
            }
        }
        
    };

})(jQuery);

$(function() {
var x = [];
x.push("hi");
$.ajaxq ("Queue1", {
    url: 'http://jsfiddle.net/echo/jsonp/',
    //dataType: "jsonp",
    type: 'post',
    data: {
        text: 'First Response',
        par1: 'another text',
        ref: x,
        delay: 3
    },
    beforeSend: function() {
        document.body.innerHTML+="Before Send 1 <br />";
    },
    error: function() {
        document.body.innerHTML+="Error 1 <br />";
    },
    complete: function() {
        document.body.innerHTML+="Complete 1 <br />";
    },
    success: function(response) {
        document.body.innerHTML+="Success 1: <br />";
    }
});
x.pop();
$.ajaxq ("Queue1", {
    url: 'http://jsfiddle.net/echo/jsonp/',
   ...