After running another function, I have retrieved a JSON object stored in the variable 'json_result'.
My objective is to log each individual JSON part (e.g. json_result[i]) after waiting for 5 seconds.
Here was my initial attempt:
for (let key in json_result) {
setTimeout(() => {
if(json_result.hasOwnProperty(key)) {
console.log(key + " -> " + JSON.stringify(json_result[key]));
};
},5000);
};
Despite using the 'let' keyword for scoping, it didn't work as expected.
In my second approach, I tried delegating the setTimeout functionality:
for (let key in json_result) {
setTimeout(() => {
if(json_result.hasOwnProperty(key)) {
delayer(key);
};
},5000);
};
function delayer(i) {
setTimeout(() => { console.log(i + "->" + JSON.stringify(json_result[i]));}, 5000)};
Can anyone explain why this isn't working and suggest a solution?
Thank you,