-1

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.

Nat
  • 300
  • 3
  • 8
  • Possible duplicate of [JavaScript closure inside loops – simple practical example](http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – Hamms Aug 24 '16 at 23:48
  • @Hamms He's not using the closure variable, he's using an object property. – Barmar Aug 24 '16 at 23:52
  • The first one only shows 0-9 because the loop is terminated on i < 10 so 'i' will never equal 10 – Niles Tanner Aug 24 '16 at 23:54
  • @NilesTanner The question is about the second version: why isn't it showing the successive values of `i`? – Barmar Aug 24 '16 at 23:55
  • It's having the same problem as the [Infamous loop issue](http://stackoverflow.com/questions/1451009/javascript-infamous-loop-issue), but he's not using `i` in the callback function. – Barmar Aug 24 '16 at 23:56
  • the Infamous loop issue which already has an answer provided by the very question I indicated. – Hamms Aug 24 '16 at 23:57
  • @Hamms But this isn't the infamous loop issue. He's not using the variable `i` in the function. – Barmar Aug 24 '16 at 23:59
  • 2
    I can't reproduce the problem. https://jsfiddle.net/barmar/7yyzjm4b/ – Barmar Aug 24 '16 at 23:59
  • 1
    Your code has a syntax error, you're missing the `)` that matches `for (`. Maybe there's something else that got lost in copying the code to SO? – Barmar Aug 25 '16 at 00:00
  • 1
    same: http://codepen.io/nilestanner/pen/xOodEX cant reproduce – Niles Tanner Aug 25 '16 at 00:01
  • Appreciate all your responses. @Barmer - Probably a copy-paste issue. My concern is not whether i goes to 9, or 10. My concern is why in the 2nd case, the bind value is not even remembered. – Nat Aug 25 '16 at 00:12
  • 1
    This is not the same as the 'JavaScript closure inside loops – simple practical example' problem. – Nat Aug 25 '16 at 00:17
  • @NilesTanner - I am not very familiar with sharing a codepen. In the external function you have there, can you put in a time-lag and try it. – Nat Aug 25 '16 at 00:18
  • If it is of any significance, my external function is a Cordova plugin call. – Nat Aug 25 '16 at 00:21

1 Answers1

0

@Niles answered and demo-ed with his code-pen correctly that external function in fact retains the bind value.

However, after hour of debugging, learned that the Cordova layer when it creates separate run-time sessions over-writes the 'this' value, and the callback is eventually called, the value used is whatever the value then is.

Nat
  • 300
  • 3
  • 8