I am trying to cycle through an array and with each value in the array, use $.getJSON to return some JSON and populate an HTML table with the return values.
I have been following this post, but seem not get this to work:
$.getJSON calls misbehaving inside a for loop
Here is my function:
$("#selectProviderTop").click(function() {
    var restURL = window.location.protocol + "//" + window.location.hostname + (window.location.port == "" ? "" : (":" + window.location.port)) + "/restGetProvider/";
    var selected = [];
    var providerKey;
    var providerID;
    var providerLegacyID;
    var providerName;
    var finalURL;
    var tr;
    // First, create an array from the User Provider Keys...
    var userProviderKeys = $("#hiddenUserProviderKeys").val();
    selected = userProviderKeys.split(",");
    console.log("selected: " + selected);
    var tableHTML = "";
    var focus = $("<div></div>"); // either match an existing element or create one: '<div />'
    var arrayLength = selected.length;
    for (var i = 0; i < arrayLength; i++) {
        (function(i) {
            console.log("i: " + i);
            providerKey = selected[i];
            console.log("providerKey: " + providerKey);
            // Get that provider and populate the table...
            finalURL = restURL + providerKey;
            console.log("finalURL: " + finalURL);
            focus.queue('apicalls', function(next) {
                $.getJSON(finalURL, function(jsonObject) {
                    tableHTML += "<tr>";
                    tableHTML += "<td><a href=\"#\" onclick='selectProvider(\"" + providerKey + "\")'>" + jsonObject["providerName"] + "</a></td>";
                    tableHTML += "<td>" + jsonObject["providerID"] + "</td>";
                    tableHTML += "<td>" + jsonObject["providerLegacyID"] + "</td>";
                    tableHTML += "</tr>";
                    console.log("tableHTML: " + tableHTML);
                    next();
                });
            });
        })(i);
    }
    // Replace table’s tbody html with tableHTML...
    console.log("final tableHTML: " + tableHTML);
    $("#tableProviderSelect tbody").html(tableHTML);
    $('#modalSelectProviderForPTP').modal('show');
});
The userProviderKeys value is 0be32d8057924e718a8b6b4186254756,2dc5f826601e4cc5a9a3424caea4115f
The code never makes the $.getJSON call it just completes the for loop.
How do I update this code to get the first value in the array, grab the JSON, create the HTML, and then cycle through the loop?
I have tried setTimeout but that didn't help me out.
If you have some ideas, could you update my existing code - I understand better when I see the code itself. Thanks.
 
    