I have a jQuery .each
statement that loops through a set of accordion/panels
$("div.section-data-source").each(function () {
populateAccordion($(this));
});
For each element, I call a function (populateAccordion
) which has an AJAX query with it's own done() callback.
How can I tell my each function to wait until the populateAccordion()
function is completed before moving on to the next iteration? Can I have a deferred against the function itself - or is there a way for the function to tell the each to iterate?
function populateAccordion(el) {
var apiName = el.attr("data-source-api-name");
$.ajax({
type: 'GET',
url: api_URL + apiName + "/" + id,
contentType: "application/json; charset=utf-8",
dataType: "json"
}).done(function(data) {
el.parent().find('.data-field').map(function () {
apiDataField = $(this).attr("data-field");
var itemValue = getApiDataValue(data, apiDataField, "Date");
$(this).text(itemValue);
});
});
console.log("FINISHED POPULATE");
};