I recently came across a similar discussion on Stack Overflow, but it involved using JQuery which I'm not using.
My issue is that I need to ensure my JSON data is fully loaded before calling my function. I understand the concept of using a callback, but for some reason, I can't seem to implement it correctly.
Here's a snippet of the JSON data (portion truncated for brevity):
{type: "robot", nom: "445250sup01", ville: "RENNES", departement: "35", region: "Ouest", …}
{type: "robot", nom: "445250sup02", ville: "PARIS", departement: "75", region: "Ile-de-France", …}
{type: "robot", nom: "445250sup13", ville: "ORLEANS", departement: "45", region: "Ouest", …}
Below is the code snippet where I call the XHR function and read the JSON:
var getDatas = getXHR(), // xhr in another file
regions = {};
dateRange = [];
getDatas.open("GET", "./db/datas.json", true);
getDatas.send();
getDatas.onreadystatechange = function() {
if (getDatas.readyState === 4 && (getDatas.status === 200 || getDatas.status === 0)) {
var robotsList = JSON.parse(getDatas.responseText);
getRobotsDatas(robotsList);
}
};
function getRobotsDatas(robotList) {
for (var i = 0; i < robotList.length - 1; i++) {...}
The issue I'm facing is that the last object in the JSON data is never loaded... Can someone guide me on the correct approach to resolve this?
Thank you in advance!