I have been trying to learn callback functions and I've spent a few days but I'm not able find a working example of using callback in nested loops. I have provided a sample code here in the fiddle where data should be pushed for each of the value.But all it is returning an empty array.
I want data to have i value , j times in it while the loop doesn't work in synch.So an empty array is being returned
I'll be using the concept in project where timeout will be replaced by sqlite insertion and select.This is an example just to know how to use it within loops.
var data = [];
for(var i = 0;i<100;i++) {
loop(i);
}
function loop(i) {
for(var j =0;j<200;j++) {
p(i);
}
}
function p(val) {
setTimeout(function(){
data.push(val);
},10)
}
console.log(data);
Here is the example of a working fiddle.
This question is similar to mine but I'm not able understand how to use it in my case.
I just want to get array having (iXj) values in data variable Thanks in advance.