function getHtnData(callback) {
// load the HTN data from fetchmapdata.php
console.log("trying to get the data...");
…
error: function(jqxhr, status, exception) {
console.log('Exception:', exception);
}
});
console.log("at end of getHtnData function ...");
}
You are correct! It seems to be an order of operations issue.
function getHtnData(callback) {
// load the HTN data from fetchmapdata.php
console.log("trying to get the data...");
$.ajax({
method: "POST",
url: "fetchmapdata.php",
data: {},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) { //r is a json object
callback(r);
console.log("data retrieved");
},
failure: function (r) {
console.log("failure");
},
error: function(jqxhr, status, exception) {
console.log('Exception:', exception);
}
});
console.log("at end of getHtnData function...")
}
function loadHtnData(variable){
console.log("loading htnData")
// using callback to process data returned by ajax
getHtnData(function(htnData){
//console.log(variable);
htnData.forEach(function(row){
var htnVariable = row[variable];
var CensusTract = row['CensusTract'];
// more code.
“loading htnData” appears in the console before “trying to get the data”.
I thought that using the callback would make loadingHtnData happen after the ajax call.