Javascript 为什么这个延迟的WHILE循环会导致大量内存泄漏?
我从这个网站的其他地方撕下了一个很好的技巧,它允许你延迟循环的周期。我对它进行了一些修改,以供我自己使用,使用的是在Chrome版本34.0.1847.131 m的TamperMonkey上运行的Javascript机器人 无论出于何种原因,只要调用循环,就会发生大规模内存泄漏(大约每秒40000k-80000k),并且根本不会循环。我的电脑已经死机两次了,因为我既没有准备也没有预料到 功能 有什么事是我搞砸的吗?我以前从未处理过内存泄漏问题Javascript 为什么这个延迟的WHILE循环会导致大量内存泄漏?,javascript,while-loop,settimeout,tampermonkey,Javascript,While Loop,Settimeout,Tampermonkey,我从这个网站的其他地方撕下了一个很好的技巧,它允许你延迟循环的周期。我对它进行了一些修改,以供我自己使用,使用的是在Chrome版本34.0.1847.131 m的TamperMonkey上运行的Javascript机器人 无论出于何种原因,只要调用循环,就会发生大规模内存泄漏(大约每秒40000k-80000k),并且根本不会循环。我的电脑已经死机两次了,因为我既没有准备也没有预料到 功能 有什么事是我搞砸的吗?我以前从未处理过内存泄漏问题 只要connected等于true,while循环就
只要connected等于true,while循环就会不断地触发,其间没有延迟。while循环不是异步的——它将暂停脚本中正在进行的所有其他操作,直到结束 把循环想象成自动代码编写器。它编写的函数的持续时间与括号中的语句为false的时间相同
while (connected == true) {
testConnection();
}
相当于写作:
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
...
只要connected等于true,就会反复出现。因此,如果connected保持等于true,while循环将尝试编写无限长的代码,导致无限循环,直到testConnection()函数使connected等于false。如果您的计算机有无限内存,那么这个无限长的代码将占用所有内存,但您的计算机没有无限内存,因此它会崩溃。只要connected等于true,while循环将不断重复启动,其间不会有延迟。while循环不是异步的——它将暂停脚本中正在进行的所有其他操作,直到结束 把循环想象成自动代码编写器。它编写的函数的持续时间与括号中的语句为false的时间相同
while (connected == true) {
testConnection();
}
相当于写作:
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
...
只要connected等于true,就会反复出现。因此,如果connected保持等于true,while循环将尝试编写无限长的代码,导致无限循环,直到testConnection()函数使connected等于false。如果您的计算机有无限内存,那么这个无限长的代码将占用所有内存,但您的计算机没有无限内存,因此它会崩溃。只要connected等于true,while循环将不断重复启动,其间不会有延迟。while循环不是异步的——它将暂停脚本中正在进行的所有其他操作,直到结束 把循环想象成自动代码编写器。它编写的函数的持续时间与括号中的语句为false的时间相同
while (connected == true) {
testConnection();
}
相当于写作:
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
...
只要connected等于true,就会反复出现。因此,如果connected保持等于true,while循环将尝试编写无限长的代码,导致无限循环,直到testConnection()函数使connected等于false。如果您的计算机有无限内存,那么这个无限长的代码将占用所有内存,但您的计算机没有无限内存,因此它会崩溃。只要connected等于true,while循环将不断重复启动,其间不会有延迟。while循环不是异步的——它将暂停脚本中正在进行的所有其他操作,直到结束 把循环想象成自动代码编写器。它编写的函数的持续时间与括号中的语句为false的时间相同
while (connected == true) {
testConnection();
}
相当于写作:
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
...
只要connected等于true,就会反复出现。因此,如果connected保持等于true,while循环将尝试编写无限长的代码,导致无限循环,直到testConnection()函数使connected等于false。如果你的计算机有无限的内存,那么这个无限长的代码将占用所有内存,但是你的计算机没有无限的内存,因此它会崩溃。我怀疑你的
while
应该是If
。一旦进入,while循环将永远不会退出。从技术上讲,这不是内存泄漏,而是(实际上)一个错误无限循环。我怀疑你的while
应该是if
。一旦进入,while循环将永远不会退出。从技术上讲,这不是内存泄漏,而是(实际上)无限循环。我怀疑你的while
应该是if
。一旦进入,while循环将永远不会退出。从技术上讲,这不是内存泄漏,而是一个错误(实际上)无限循环。我怀疑您的while
应该是if
。一旦进入,while循环将永远不会退出。从技术上讲,这不是内存泄漏,而是(实际上)错误无限循环。我明白了,我的问题是我在想我需要在setDelay
中有某种循环。出于某种原因,我完全错误地将我复制的代码中的IF
语句理解为FOR
循环。我犯了一个非常愚蠢的错误。感谢所有回答/评论你的人,你是完全正确。我明白了,我的问题是我在想我需要在setDelay
中有某种循环。出于某种原因,我完全错误地将我复制的代码中的IF
语句理解为FOR
循环。我犯了一个非常愚蠢的错误。感谢所有回答/评论的人,你们都是正确。我明白了,我的问题是我在想我需要在setDelay
中有某种循环。出于某种原因,我完全错误地将我复制的代码中的IF
语句理解为FOR
循环。我犯了一个非常愚蠢的错误。感谢所有回答/评论的人,你们都是参与者正确。我明白了,我的问题是我在想我需要在setDelay
中有某种循环。出于某种原因,我完全错误地将我复制的代码中的IF
语句理解为FOR
循环。我犯了一个非常愚蠢的错误。谢谢