I want to create X amount of random objects (100 in this example) in NodeJS. These objects have random values plus I want them created at randomIntervals. In order to accomplish this I wrote code similiar to this:
i=0;
while (i < 100){
randomNum1 = Math.floor(Math.random()*100);
randomNum2 = Math.floor(Math.random()*100);
randomIntervalTime = Math.floor(Math.random()*100);
timeOutInterval = setTimeout(function(){
return new SomeObject(randomNum1, randomNum2)}, randomIntervalTime);
i++;
}
The issue is that the value aren't preserved for the callback and it will just generate 100 new objects with all the same values (the last values created in the loop)
I realize this is because of some issue with the way callbacks work that I don't totally grasp, but my question is how would someone accomplish this in Nodejs.