Javascript、函数、循环、变量和其他东西

标签 javascript

我知道这种问题已经被问了一遍又一遍(我在 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);

        .....

    }
        .....
  }

}

最佳答案

看起来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);
}

关于Javascript、函数、循环、变量和其他东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12389342/

相关文章:

javascript - 单击文本框时,文本框的边框应该是透明的

javascript - 网页上桌面和 iPad 的图像宽度和高度不同?

javascript - 隐藏和显示输入不起作用

javascript - React - 带有确认从数组映射中删除的模态

javascript OO 如何使用一些 JSON 变量更新自身参数

javascript - 基于时区重定向用户

javascript - 删除部分文本

Javascript:具有用于所有方法调用的单个处理程序的对象

javascript - 将 Javascript 字符串转换为特定格式

javascript - Jquery/AJAX PHP 域检查器