简体   繁体   English

为什么这个javascript代码有无限循环?

[英]Why does this javascript code have an infinite loop?

optionElements is a 2d array. optionElements是一个二维数组。 Each element has an array of length 2. These are an integer number and an element. 每个元素都有一个长度为2的数组。这些是一个整数和一个元素。 I have a select list called linkbox, and i want to add all of the elements to the select list. 我有一个名为linkbox的选择列表,我想将所有元素添加到选择列表中。 The order I want them to go in is important, and is determined by the number each element has. 我希望他们进入的顺序很重要,并且由每个元素的数量决定。 It should be smallest to highest. 它应该是最小的到最高的。 So think of it like this: 所以想想这样:

optionElements is: optionElements是:

[ [5, <option>], [3, <option], [4, <option], [1, <option], [2, <option]]

and it would add them to link box in order of those numbers. 它会按照这些数字的顺序将它们添加到链接框中。 BUT that is not what happens. 但这不是发生的事情。 It is an infinite loop after the first time. 这是第一次之后的无限循环。 I added the x constraint just to stop it from freezing my browser but you can ignore it. 我添加了x约束只是为了阻止它冻结我的浏览器,但你可以忽略它。

var b;
var smallest;
var samllestIndex;
var x = 0;
while(optionElements.length > 0 && ++x < 100)
{
    smallestIndex = 0;
    smallest = optionElements[0][0];
    b = 0;
    while( ++b < optionElements.length)
    {
        if(optionElements[b][0] > smallest)
        {
            smallestIndex = b;
            smallest = optionElements[b][0];
        }                    
    }                    
    linkbox.appendChild(optionElements[smallestIndex][1]);
    optionElements.unshift(optionElements[smallestIndex]);
}

can someone point out to me where my problem is? 有人能指出我的问题在哪里吗?

Update 更新

forgot to add the > sign is wrong in the while loop but is not the cause of the problem. 忘了添加>符号在while循环中是错误的,但不是问题的原因。

It's an infinite loop because unshift adds the array you're passing in to the one you're calling it on. 这是一个无限循环,因为unshift会将您传入的数组添加到您正在调用的数组中。 So the outer loop checks if optionElements has more items than 0, then at the end it gets larger, so that loop never exits. 因此外部循环检查optionElements是否有多于0的项,然后在最后它变大,以便循环永远不会退出。 http://www.w3schools.com/jsref/jsref_unshift.asp http://www.w3schools.com/jsref/jsref_unshift.asp

I'm also not sure why you're doing it this way in any case. 在任何情况下,我也不确定你为什么这样做。 Why not just sort the optionElements array first, then iterate over it once to add each element to the linkbox? 为什么不首先对optionElements数组进行排序,然后迭代一次以将每个元素添加到链接盒中?

from javascript unshift() function : 来自javascript unshift()函数

The unshift() method adds new elements to the beginning of an array, and returns the new length. unshift()方法将新元素添加到数组的开头,并返回新的长度。

This means you keep adding elements to the beginning instead of removing them, giving your infinite loop. 这意味着你不断添加元素而不是删除它们,给你无限循环。

Try using a counter to run the loop optionElements.length timesbetter performance). 尝试使用计数器运行循环optionElements.length倍于性能)。 Or you can use shift() to get the desired effect: 或者你可以使用shift()来获得所需的效果:

optionElements.shift();

:D :d

I can tell you that .unshift() doesn't work in IE. 我可以告诉你.unshift()在IE中不起作用。 So you might want to test in Mozilla/Safari if you were using IE before. 因此,如果您之前使用的是IE,则可能需要在Mozilla / Safari中进行测试。

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

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