I know that this problem is related to JS scope and I have searched, but can't get the solutions from other stackoverflow questions to work.
I have this program
function write(x, y) {
console.log(x);
console.log(y);
}
var data = {
"property": {
"1": {
"values": [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
},
"2": {
"values": [[11, 12], [13, 14], [15, 16], [17, 18], [19, 20]]
}
}
}
var delay = 1000;
for (var x in data.property) {
for (var i = 0; i < data.property[x].values.length; i++) {
var one = data.property[x].values[i][0];
var two = data.property[x].values[i][1];
setTimeout(function() {
write(one, two);
}, delay);
delay += 1000;
}
}
that reads data from an object and loops over both the object and the arrays in it. I want it to print the array values one second apart, but it always prints the values from the last iteration. I have tried with closures as suggested in the other question, but can't get it to work.