永不结束循环 javascript

Never ending for loop javascript

为什么这个循环永无止境?每个部分可能有也可能没有小节,这些小节与 section.The 相同 class for 语句的工作非常奇怪。它一直直接回到 list.append('<label class="heading">' + theSection.description + '</label><br/><hr><br/>');。如果没有小节,它不应该这样做。

function createAssessmentSectionFormHTML(section) {
    var list = $('#assessmentSectionForm' + platform);
     appendSection(section, list);
}

function appendSection(theSection, list) {
    list.append('<label class="heading">' + theSection.description + '</label><br/><hr><br/>');
    appendSectionQuestions(theSection, list);
    if (theSection.allSubSections) {
        for (x = 0; x < theSection.allSubSections.length; x++) {
            var theSectionA = theSection.allSubSections[x];
            appendSection(theSectionA, list);
        }
        return;
    }
}

我认为您的 for 循环中缺少 "var":

for (var x = 0; x < theSection.allSubSections.length; x++) {

这应该可以解决问题 - 它是 运行 无限,因为 x 没有声明。 undefined 总是小于任何长度,所以没有终点。