1

我知道这种问题已经被一遍又一遍地问过(我在 Stackoverflow 和其他地方读过很多遍)。我尝试了闭包、匿名函数等,但我什么也没得到,因为我不是代码大师):。

话虽如此,这是我的问题:

function A (a) {
  for (var i in a) {
    var x = a[i];
    function B (x); // wait for the end of the function B before next loop
  }
}

……

function B (x) {

 //do things with "x" and repeat the function B until a condition is true and  then return to the function A
}

我尝试了很多东西(可能还不够),但没有一个能正常工作......

所以提前谢谢你的建议

问候

编辑 :

好的,这里有完整的代码:

  var Test  = {

  ......

  setProgress: function(count) {
   var win = Test.getMainWindow();
   var progressMeter = win.document.getElementById("progressMeter");
   progressMeter.value =  count;
  }, 

  cancelCheck: function(event) {
    var win = Test.getMainWindow();
    win.document.getElementById("cancel").value = "cancel";
  },


  timeout: function (item) {

      var win = Test.getMainWindow();
      win.document.getElementById("progressMeterText").value = item;
      var stat = Test.getStatus()

       if (stat !== "loading" || win.document.getElementById("cancel").value == "cancel") {

      // if cancel button is clicked         
      if (win.document.getElementById("cancel").value == "cancel") {

        Test.setProgress(100);
        Test.stop();
        win.document.getElementById("cancel").value = "";
        win.document.getElementById("progressMeter").collapsed=true;
        win.document.getElementById("cancel").hidden=true;
        win.document.getElementById("progressMeterText").hidden=true;

      }
      // end of the loading
      else if (stat !== "loading") {
        Test.setProgress(100);
        win.document.getElementById("progressMeter").collapsed=true;
        win.document.getElementById("cancel").hidden=true;
        win.document.getElementById("progressMeterText").hidden=true;
        win.document.getElementById("cancel").value = "";
      }

      // loading is running
      else {        
        count = count+5;
        if (count == 100) {
          count = 0;
        }
        Test.setProgress(count);
        setTimeout (Test.timeout, 100 , item);
       }
     } 
   },

   getStuff: function (items) {
    for(var i in items) {
      var item = items[i];

        if (!item.host || (items && item.id != items)) continue;

        var listener = new Test.GetListener(item);
        listener.instance = new TestGet(item, listener).execute();

        count = 10;
        Test.setProgress(count);
        var buttoncancel = win.document.getElementById("cancel");
        buttoncancel.addEventListener ('click', Test.cancelCheck, false);

        Test.timeout(item);

        .....

    }
        .....
  }

}
4

2 回答 2

2

这似乎B是一个递归函数,但您仍然应该通过B();.

function A(a) {
  for (var i in a) {
    B(a[i]); 
  }
}

function B(x) {
   var ret = dosomething(x);
   if (condition(ret)) {
     return;
   }
   return B(ret);
}
于 2012-09-12T13:37:35.067 回答
0

我也不是代码专家,但我确实看到了一件事可能会有所帮助。

因为你有一个 var x 它将不断替换 var x 所以(我假设你正在使用一个数组)你将只有一个值,数组中的最后一项,作为 x 的值。因此,您的函数 B 将使用函数 A 中数组中最后一项的值运行一次。如果这不是您要查找的内容,请尝试为数组中的每个项目创建一个唯一变量,或者简单地说function B (a[i]); 使用 if/else 或 switch/case 语句仅在适当时返回您希望的信息。

我仍然掌握了js,所以我希望这至少有点帮助。祝你好运!

于 2012-09-12T13:37:37.367 回答