I am facing an issue where my JSON data is being overwritten by the result of the last XMLHttpRequest made for each country. Is there a way to prevent this from happening? Below is the code snippet in question:
function getDataBetween() {
for (var i = 0; i < CountryNames.length; i++) {
var countryName = CountryNames[i];
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
for (var a = 0; a < CountryNames.length; a++) {
dataset[a] = JSON.parse(xmlhttp.responseText);
console.log(dataset[a]);
}
}
}
xmlhttp.open("GET","update.php?country=" + countryName + "&begin=" + beginTime + "&end=" + endTime + "&functionName=getActiveUsers", true);
xmlhttp.send();
}
DrawStructure();
}
I have attempted to implement Promise syntax as suggested here, but I am struggling to apply it effectively in this scenario despite trying multiple approaches.