I have this piece of code in a module:
for (var i = 1; i <= n; i++) {
when = i * 1000;
track.execute(true, when); // <-- This line calls the method below
}
And this piece in another module:
this.execute = function(on, when){
[...]
var createCallback = function(w){ // <-- Here 'when' should be bound to this function when is called below
return function(buffers){
object.doSomething(buffers, w);
}
};
[...] // None of this code modifies when or the callback 'factory'
this.asyncObj.getBuffer(createCallback(when)); // <-- Binding ?!?
};
The problem is that the value of when
is updated at each call, therefore the callback is always executed with the latest value of when, not the one that was set at the time the method was executed (which was the expected behavior).
As you can see I've already tried the hack described in this question: JavaScript closure inside loops – simple practical example but couldn't get it right.
For some reason the callback function above always gets the latest value of when.
Thanks in advance to whoever would like to help!
PS: I guess this happens because the callback is set every time and, thus, every time is set with the latest value of when. But I'm not sure.
UPDATE: here is a tinker to try it out...And it works! O.o http://tinker.io/dee10 (this code reflects the current state of my code locally. It's the same thing as above, anyway).