I am having a problem with a classic javascript "issue"!
I want to create an array of functions that will each be set to run after different lenghs of time (each one second apart).
However the following code is creating functions that all have the timeout set to the last value of timeout.
Any help to solve this would be wonderful! Thanks!
var timeout = 0;
var myfunctions = []
urls.forEach(function(url){
var newFunction = function(callback){
(function (timeout){
setTimeout(function(){
getTransactions(url, function(err, data){
callback(err, data)
})
},timeout)
})(timeout)
}
timeout = timeout + 1000
myfunctions.push(newFunction)
})
The reason each function has a callback is that I that plan to run them all using:
async.parallel(myfunctions,
function(err, results) {
if(err) {
console.log(err.message)
}
else{
console.log(results)
}
})