简体   繁体   English

JavaScript for循环陷入无限循环

[英]Javascript for loop gets stuck in infinite loop

I have added a Javascript function as an onclick on a Boostrap button. 我添加了一个Javascript函数作为对Boostrap按钮的onclick。 When I click the button, my CPU goes to 100%, the page becomes unresponsive and the following two console lines are printed over and over again: 当我单击该按钮时,我的CPU达到100%,页面变得无响应,并且以下两行控制台行被一遍又一遍地打印:

Loop i 1 循环我1

and

Loop j 1 循环j 1

What could be the problem? 可能是什么问题呢? Here is my code: 这是我的代码:

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

Strah Behry, James Thorpe and Rajesh are all correct. Strah Behry,James Thorpe和Rajesh都是正确的。 I wrote "=" instead of "==" in my if-statesments. 我在if陈述中写了“ =”而不是“ ==”。 Question resolved. 问题已解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM