0

I got the below code within a callback of xmlHTTPRequest callback function:


// some more code before
...
// schedule the UI update
var totSteps = 6;
for(var i = 0; i < listChangeEl.length; ++i) {
  // callback pulse function
  var curPulse = function cell_pulse(elName, curCnt) {
    console.log("Accessing element: " + elName);
    var curEl = document.getElementById(elName);
    console.log("Element: " + elName + " = " + curEl);
    var curColor = RGB2HTML(255, 255*(curCnt/totSteps), 255*(curCnt/totSteps));
    if(curCnt < totSteps) {
      // recursion here!
      setTimeout( function(){ cell_pulse(elName, curCnt+1); }, 125);
    }
  };
  // start it!
  setTimeout( function() { (curPulse)(listChangeEl[i], 0); }, 125);
}

Apparently when the above is setTimeout( function() { (curPulse)(listChangeEl[i], 0); }, 125); is executed , listChangeEl[i] does contain the right id of a cell I want to update, but then, upon first execution of function cell_pulse the parameter elName is undefined.

What am I doing wrong? Does Javascript manage lambda (of lamdba) properly?

Thanks,
Ema

Emanuele
  • 1,408
  • 1
  • 15
  • 39
  • i is not closed, so this isn't the age-old loop scope question... be aware that since your function runs after the loop, i will always be listChangeEl.length when curPulse() executes. – dandavis Jul 23 '13 at 19:22
  • What a sad Javascript world... basically if I use the _string_ trick I can do it... but I guess I'll try with solution proposed in your link too.. Still... – Emanuele Jul 23 '13 at 19:34
  • 1
    oops, missed the extra "}" at the end. simply add "(function(i){" to the top of the loop and "}(i));" to the end of it... – dandavis Jul 23 '13 at 19:43

0 Answers0