gpt4 book ai didi

javascript - JS Loop 循环并导致浏览器崩溃?

转载 作者:行者123 更新时间:2023-12-03 05:01:53 26 4
gpt4 key购买 nike

for (i=0;i<channelName.length;i++) {
if (channelName[i]=="channel"||channelName[i]=="user") {
checkUserDuplicate(channelName[i]);
}
}

此循环导致所有浏览器中出现“内存不足”崩溃。谁能明白为什么吗?它似乎在 IF 语句处崩溃,然后以某种方式导致无限循环。

如果您想知道代码的作用,它会在未定义长度的数组中查找关键字“channel”和“user”,然后获取下一个位置的字符串。

任何帮助将不胜感激,因为我已经困惑地坐在这里 2 个小时了。

编辑:channelName 是一个类似 http://www.youtube.com/user/username 的 URL

这是函数:

function checkUserDuplicate(channelName) {
var idarray=[];match=0;$('.channels').each(function(){idarray.push(this.id)});
for (i=0;i<idarray.length;i++) {
var current=channelName.toLowerCase();compare=idarray[i].toLowerCase();
if (current==compare) {callError(channelName+" already exists in this collection");match=1;}
} if (match==0) {checkExists(channelName);}

}

真是一团乱:)

最佳答案

在您的for循环中,如果您没有指定var i = 0;,那么它就可以全局访问。在您的其他函数中,您可以修改 i 的值并导致它不会按预期增加,最终导致无限循环。

抱歉,我没有阅读代码,因为它非常困惑,但这可能就是原因

关于javascript - JS Loop 循环并导致浏览器崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42191844/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com