Javascript for循环卡在无限循环中

时间:2016-06-15 08:29:49

标签: javascript for-loop web infinite-loop

我在Boostrap按钮上添加了一个Javascript函数作为onclick。当我单击按钮时,我的CPU变为100%,页面变得无响应,并且一遍又一遍地打印以下两个控制台行:

  

循环i 1

  

循环j 1

可能是什么问题?这是我的代码:

var strBeskrivningPH = "random string";

    var row = document.createElement("tr");
        //var i indicates which cell index (arranged horizontally inside a row) 
        for(var i = 0; i < 13; i++){
            console.log("Loop i 1");
            var cell = document.createElement("td");
                if(i=0){
                    var cellContent1 = document.createElement("INPUT");
                    cellContent1.setAttribute("type", "textbox");
                    cellContent1.setAttribute("placeholder", strBeskrivningPH);
                    cell.appendChild(cellContent);
                }
                else if(i=1){
                    var cellContent2 = document.createElement("SELECT");
                    cellContent2.setAttribute("class", "list");
                    //cellContent2.onchange = setprog; //fix function later for changing option here. It will auto-set some checkboxes
                        for(var j = 0; j<13; j++){
                            console.log("Loop j 1");
                            var option = document.createElement("option");
                            var oText = "";
                            //TODO: kolla om hämta detta från lista istället via API?
                            if(j==0){
                                oText = "a";
                            }
                            else if (j==1){
                                oText = "b";
                            }
                            else if (j==2){
                                oText = "c";
                            }
                            else if (j==3){
                                oText = "d";
                            }
                            else if (j==4){
                                oText = "e";
                            }
                            else if (j==5){
                                oText = "f";
                            }
                            else if (j==6){
                                oText = "g";
                            }
                            else if (j==7){
                                oText = "h";
                            }
                            else if (j==8){
                                oText = "i";
                            }
                            else if (j==9){
                                oText = "j";
                            }
                            else if (j==10){
                                oText = "k";
                            }
                            else if (j==11){
                                oText = "l";
                            }
                            else if (j==12){
                                oText = "m";
                            }
                            //var option = document.createElement("option");
                            option.text = oText;
                            option.value = j;
                            cellContent2.add(option, null);
                        }
                        cell.appendChild(cellContent2);
                }
                else if(i=8){
                    var goodnight = document.createElement("select");
                    goodnight.setAttribute("class", "list");
                    var goodnightOptions = ["0% a", "25% a", "50% a", "75% a", "100% a", "0%", "25%", "50%", "75%", "100%"];
                    for(var j = 0; j < goodnightOptions.length; j++){
                        console.log("Loop j 2");
                        var option = document.createElement("option");
                        option.text = goodnightOptions[j];
                        options.value = j;
                        goodnight.add(option, null);
                    }
                    cell.appendChild(goodnight);
                }
                else if(i = 9){
                    var goodmorning = document.createElement("select");
                    goodmorning.setAttribute("class", "list");
                    var goodmorningOptions = ["0% a", "25% a", "50% a", "75% a", "100% a", "0%", "25%", "50%", "75%", "100%"];
                    for(var j = 0; j < goodnightOptions.length; j++){
                        console.log("Loop j 3");
                        var option = document.createElement("option");
                        option.text = goodmorningOptions[j];
                        options.value = j;
                        goodmorning.add(option, null);
                    }
                    cell.appendChild(goodmorning);
                }
                else{
                    var checkbox = document.createElement("INPUT");
                    checkbox.setAttribute("type", "checkbox");
                    //SET ID
                    cell.appendChild(checkbox);
                }
                row.appendChild(cell);
        }
        console.log(5);
        //document.getElementById("tableBodyId").innerHTML += row;
        document.getElementById("tableBodyId").appendChild(row);
}

1 个答案:

答案 0 :(得分:0)

Strah Behry,James Thorpe和Rajesh都是正确的。我在if-statesments中写了“=”而不是“==”。问题解决了。