Welcome Guest, Not a member yet? Register   Sign In
What's the standard way of dealing with the ordering of multiple ajax requests?
#5

you can disable async if you will be using loop to execute your ajax request in order just set async: false to your $.ajax, its the easiest way but its a very bad idea. If you want to work with asynchronous ajax request forget about loops Smile use callback instead. store your series of reques inside the array in order of your choice.

Code:
var aSeriesOfRequest = [
    {
        fruit: 'apple',
        color: 'red'
    },
    {
        fruit: 'banana',
        color: 'yellow'
    },
    {
        fruit: 'carrots',
        color: 'orange'
    }
];

var iRequestLength = aSeriesOfRequest.length;

var onRequest = function(oCallback, iIndex = 0) {
    if (iIndex < iRequestLength) {
        $.ajax({
            method: 'POST',
            request: aSeriesOfRequest[iIndex],
            success: function() {
                oCallback(oCallback, iIndex++);
            }
        });
    }
}

onRequest(onRequest);

I'm not sure if its a valid javascript for the reason that i'm on mobile and doesnt have a good text editor, im not sure jquery's ajax request just read the docs to make sure.

My point here is just make function that calls it self if the array length is not reach. Hope you got it.
God Bless CI Contributors Smile
Reply


Messages In This Thread
RE: What's the standard way of dealing with the ordering of multiple ajax requests? - by marksman - 05-03-2017, 02:49 PM



Theme © iAndrew 2016 - Forum software by © MyBB