JSFiddle - React, Tailwind, and code Playground

by seboulba

HTML

<script src="https://raw.github.com/sebastien-roch/Jquery-Async-queue/master/jquery.asyncqueue.js"></script>

JavaScript

/*
* This file is part of the jquery plugin "asyncQueue".
*
* (c) Sebastien Roch <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
(function($){
    $.AsyncQueue = function() {
        var that = this,
            queue = [],
            failureFunc,
            paused = false,
            lastCallbackData,
            _run;

        _run = function() {
            var f = queue.shift(),
                args = [];

            if (f) {
                args.push(that);
                // make sure the current lastcallback is not used
                // after we call the next function
                if (typeof lastCallbackData !== 'undefined' && lastCallbackData instanceof Array) {
                    args = args.concat(lastCallbackData);
                    lastCallbackData = undefined;
                }

                f.apply(that, args);

                if (paused === false) {
                    _run();
                }
            }
        }

        this.getq = function() {
            return queue;
        }

        this.onFailure = function(func) {
            failureFunc = func;
        }

        this.add = function(func) {
            queue.push(func);
            return this;
        }

        this.storeData = function() {
            lastCallbackData = [];
            for (var i = 0; i < arguments.length; i++) {
                lastCallbackData.push(arguments[i]);
            }
            return this;
        }

        this.run = function() {
            paused = false;
            _run();
        }

        this.pause = function () {
            paused = true;
            return this;
        }

        this.failure = function() {
            paused = true;
            if (failureFunc) {
                var args = [that];
                for(i = 0; i < arguments.length; i++) {
                    args.push(arguments[i]);
               ...