异步(setTimeout)lambda不使用正确的输入

时间:2013-07-23 19:15:56

标签: javascript asynchronous settimeout lambda

我在xmlHTTPRequest回调函数的回调中获得了以下代码:


// 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);
}

显然当上面的setTimeout( function() { (curPulse)(listChangeEl[i], 0); }, 125);被执行时,listChangeEl[i] 确实包含我要更新的单元格的正确ID,但是在第一次执行{{ 1}}参数 elName undefined

我做错了什么? Javascript是否正确管理 lambda lamdba )?

谢谢,
EMA

0 个答案:

没有答案