简体   繁体   English

javascript函数陷入无限循环

[英]javascript Function runs into infinite loop

The following code runs into an infinite loop. 以下代码遇到无限循环。 Why? 为什么? How do I prevent that? 我该如何预防?

The problem seems to be the $('.skill-levels').append(formattedskillvalue); 问题似乎是$('.skill-levels').append(formattedskillvalue); line. 线。

var HTMLskillstart = '<ul class="skills"></ul>';
var HTMLskill = '<li class="skill-item">%data%</li>';
var HTMLskillLevel = '<div class="skill-levels"></div>';
var HTMLlevel = '<div class="level" width="%data%"></div>';

var skills = {
    'Html': '70%',
    'css': '60%',
    'javascript': '50%',
    'C': '70%',
    'C++': '60%'
};


skills.display = function() {
    var i = 0;
    $.each(skills, function(key, value) {
        $('.skillset').append(HTMLskillstart);
        var formattedskill = HTMLskill.replace("%data%", key);
        var formattedskillvalue = HTMLlevel.replace("%data%", value);
        $('.skills:last').append(formattedskill, HTMLskillLevel);
        $('.skill-levels').append(formattedskillvalue);
        i++;
        console.log(i);
    });
}

skills.display();

It's a fairly subtle problem: display is a property of skills , so of course $.each will eventually call your callback with key = "display", value = (the function) . 这是一个相当细微的问题: displayskills的属性,因此当然$.each最终将使用key = "display", value = (the function)调用回调。 Then when you do: 然后,当您这样做时:

var formattedskillvalue = HTMLlevel.replace("%data%", value);

replace sees a function, and so it calls it. replace看到一个函数,因此将其调用。 ( replace supports you using a function for the replacement rather than a string.) So you end up recursing forever. replace支持您使用函数进行替换而不是使用字符串。)因此,您最终将永远递归。

Either don't make display a property of skills , or make it non-enumerable, or put a guard in the $.each callback that skips the code for key == "display" . 要么不使display成为skills的属性,要么不使其成为不可枚举的对象,要么$.each回调中设置防护,以跳过key == "display"的代码。

Here's an example with a non-enumerable display : 这是一个不可枚举的display示例:

 var HTMLskillstart = '<ul class="skills"></ul>'; var HTMLskill = '<li class="skill-item">%data%</li>'; var HTMLskillLevel = '<div class="skill-levels"></div>'; var HTMLlevel = '<div class="level" width="%data%"></div>'; var skills = { 'Html': '70%', 'css': '60%', 'javascript': '50%', 'C': '70%', 'C++': '60%' }; Object.defineProperty(skills, "display", { value: function() { $.each(skills, function(key, value) { $('.skillset').append(HTMLskillstart); var formattedskill = HTMLskill.replace("%data%", key); var formattedskillvalue = HTMLlevel.replace("%data%", value); $('.skills:last').append(formattedskill, HTMLskillLevel); $('.skill-levels').append(formattedskillvalue); }); } }); skills.display(); 
 <div class="skillset"></div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Hey all I found the answer . 嘿,我找到答案了。 $.each(skills, function(key, value) this code iterates through the objects key value pairs. $ .each(skills,function(key,value)此代码迭代对象的键值对。

skills.display() is also a part of the object hence at the end of the loop it gets called and gets appended . skill.display()也是对象的一部分,因此在循环结束时它被调用并被追加。 This in turn makes the loop run again causing the program to enter infinite loop 这又使循环再次运行,导致程序进入无限循环

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

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