简体   繁体   English

永无止境的循环JavaScript

[英]Never ending for loop javascript

Why is this loop never-ending? 为什么这个循环永无止境? Every section may or may not have subsections, which are the same class as a section.The for statement is working very strangely. 每个节可能有也可能没有子节,这些子节与节是同一类。for语句的工作方式很奇怪。 It goes straight back up to list.append('<label class="heading">' + theSection.description + '</label><br/><hr><br/>'); 它直接返回到list.append('<label class="heading">' + theSection.description + '</label><br/><hr><br/>'); all the time. 每时每刻。 If there are no subsections it should not do that. 如果没有小节,则不应这样做。

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

I believe you are missing a "var" in your for loop: 我相信您在for循环中缺少“ var”:

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

This should solve the problem - it's running infinitely because x is not declared. 这应该可以解决问题-它可以无限运行,因为未声明x。 Undefined is always less than any length, so there's no end point. 未定义的长度总是小于任何长度,因此没有终点。

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

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