While this works, and prints i from 0 to 9
for (var i=0; i < 10; i++ {
function myFunction (callback) {
setTimeout(function () {
callback('Answering your phone call');
}, 10000);
}
myFunction(function (message) {
console.log("i = " + this.i + " , message = " + message);
}.bind({i: i}));
}
This does not work, and prints i as 10, 10 ,10, ... 10 (10 times)
for (var i=0; i < 10; i++ {
// There is an externalFunction, which is a Cordova call, that does call the callBack function with a message.
externalFunction(function (message) {
console.log("i = " + this.i + " , message = " + message);
}.bind({i: i}));
}
What am I doing wrong? I do not have control over the externalFunction, and I want to retain the call # when the callBack does return.
I have tried various versions of closures, and binds and cannot get this to work.