【问题标题】:Why is this javascript looping infinitely?为什么这个javascript无限循环?
【发布时间】:2012-04-07 19:58:45
【问题描述】:

constructBuilder() 方法不应无限执行,因为它设置为仅循环 10 次,并且 data.length 的值永远不会改变。

这个循环和方法实际上工作得很好,直到我在循环中调用另一个方法。

当我在这个循环中调用getOptions(type)方法时,i的值变化很奇怪,而且总是遵循这种模式:

1st run: i=0
2nd run: i=1
3rd run: i=3
4th run: i=5
5th run: i=6
6th run: i=4
7th run: i=4
8th run: i=4
nth run: i=4

i 的值卡在 4,不递增,循环无限运行!

为什么会这样??

代码如下:

var data = [["Text Array", "Some more text", "btnText", "btn2text"],
            ["Text2", "2: more text", "btnText2", "btn2text2"],
            ...
           ];

var products, order;

function initialise() {
    products = loadProducts();
    order = new Order();
    constructBuilder();
}

function constructBuilder() {
    var qb_boxes_innerHTML = "";
    for (i=0; i<data.length; i++) {
      alert("i="+i + "; data length="+data.length);
      var box_innerHTML = "<table width=100% height=100% cellpadding=0; cellspacing=0; border=0>";
      box_innerHTML += "<tr><td width=100% height=\"50px\">" + data[i][0] + "</td></tr>";
      box_innerHTML += "<tr><td width=100% class=\"scroll\" valign=\"top\">" + data[i][1] + getOptions(i) + "</td></tr>";
      box_innerHTML += "<tr><td width=100% height=\"50px\" align=\"right\" valign=\"middle\"><form action=\"javascript:next();\"><input type=\"button\" value=\""
                    + data[i][2] + "\" onClick=\"prev();\"/><input id=\"continueBtn\" type=\"submit\" value=\""
                    + data[i][3] + "\" disabled/></form></td></tr>";
      box_innerHTML += "</table>";
      qb_boxes_innerHTML += "<div id=\"qb_box" + i + "\" class=\"qb_box\" style=\"visibility: hidden;\">" + box_innerHTML + "</div>";
    }
    document.getElementById("qb_boxes").innerHTML = qb_boxes_innerHTML;
    document.getElementById("qb_box0").style.visibility = "";
}
function getOptions(type) {
    var optionsList = getProducts(products, type);
    var options_html = "";
    for (i=0; i<optionsList.length; i++) {
      options_html += "<input id=\"check"+type+"_"+i+"\" type=\"checkbox\"/>" + optionsList[i].name + "<BR/>";
    }
    return options_html;
}

function getProducts(productList, type) {
      var productsOfType = new Array();
      for (i=0; i<productList.length; i++) {
        if (productList[i].type == type)
            productsOfType.push(productList[i]);
      }
      return productsOfType;
}

如果您需要更多信息,请发表评论。

感谢收看。

【问题讨论】:

    标签: javascript loops infinite-loop


    【解决方案1】:

    通过使用i 而不使用var,您实际上是在使用全局变量window.i。更改您的函数,以便将 i 声明为局部变量:

    function constructBuilder() {
        var qb_boxes_innerHTML = "";
        var i;
        /* ... */
    }
    

    【讨论】:

    • "use strict"; 的喜悦:)。
    • @Zeta 谢谢,就是这样。如果有答案,我会接受。
    • @user1031312:不客气。您可以使用"use strict"; 避免此类错误(请参阅strict mode on MDN)。
    【解决方案2】:

    您的i 变量是全局变量,因此每个函数内的循环共享同一个计数器。在 for 循环声明 (for(var i = 0; ...) 中使用 var 关键字来声明局部变量。

    【讨论】:

    • 谢谢,我提高了你的回答,但 Zeta 比你快了大约 30 秒 :)
    【解决方案3】:

    问题是在constructBuilder() 中调用getOptions(i)。当您在全球范围内声明“i”时,在您的情况下,i 的值不断变回 4

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 2020-08-08
      • 2012-05-02
      • 2011-11-15
      • 2018-03-17
      • 1970-01-01
      • 2019-10-25
      • 2012-11-01
      • 2011-02-19
      相关资源
      最近更新 更多