1

My issue: AJAX requests are interfering with each other, even though I am not using global variables, variables like folderlist seem to contain items that they should not contain. If I set async to false, the issue goes away, however this is bad as it makes the end user think the page is locked up.

Items that belong in the folderList in one iteration show up in a different iteration, and shouldn't be there.

for (var g = 0; g < args['url'].length; g++) {

    var folderList = [];
    var url = args['url'][g];
    var serverUrl = args['url'][g];
    $.getJSON(url + "?f=pjson", function (json) {
        for (var key in json.folders) {
            folderList[key] = url + "/" + json.folders[key];
        }
    }).done(function () {
        for (var i = 0; i < folderList.length; i++) {
            secondAjaxCall(folderList[i], serverUrl);
        }
    })
}

function secondAjaxCall() {

     $.getJSON(url + "?f=pjson", function (json) {
    }).done(function (json) {
        for (var i = 0; i < json.whatever.length; i++) {
           thirdAjaxCall();
        }

I've looked into the jquery when and deferred, however none of these will stop the next iteration from starting until it's done all the requests it needs to do.

What I need is a way for the next iteration to wait until the first iteration is done.

Evan Parsons
  • 901
  • 17
  • 28
  • read about waterfall: https://github.com/dio-el-claire/jquery.waterfall – num8er Jul 20 '15 at 16:08
  • I got around this the other day by creating a for loop before the AJAX call and setting the variable(s) I needed to pass as an array of values that I would have otherwise passed upon each loop. Then I sent that array to PHP, set the array to be a PHP array: http://stackoverflow.com/questions/22730989/convert-jquery-array-string-to-php-array, then from there I returned a nice JSON object back to my AJAX function and did something with it on success. (`json_encode`, `json_decode` and `dataType: 'json'` were key) – ctwheels Jul 20 '15 at 16:12
  • Why not use https://api.jquery.com/jquery.when/ ? – NorCalKnockOut Jul 20 '15 at 16:19

0 Answers0