在 Javascript 中进入无限循环和递归调用时如何调试 javascript?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12815892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to debug javascript when it goes into infinite loops and recursive calls in Javascript?
提问by lkahtz
When you are in the infinite loop or recursive calls, basically the browser stops responding to anything (either on Chrome or FF). You cannot see logs, cannot get into debugger, even you cannot open the console itself. The browser just simply freezes. This is so annoying. It seems I can do nothing but sitting here scratching my head... Anyone can shed light on how to solve this?
当您处于无限循环或递归调用中时,浏览器基本上会停止响应任何内容(在 Chrome 或 FF 上)。您无法查看日志,无法进入调试器,甚至无法打开控制台本身。浏览器只是简单地冻结。这太烦人了。似乎我只能坐在这里挠头……任何人都可以解释如何解决这个问题?
回答by Tigraine
Another trick you could try is to have the Web developer tools in Chrome open and try to hit Pausewhen the Browser apparently hangs. Then it should break at the line where it's currently executing. With some stepping out you should get to the bottom of this.
您可以尝试的另一个技巧是打开 Chrome 中的 Web 开发人员工具,并在浏览器明显挂起时尝试点击暂停。然后它应该在它当前正在执行的行处中断。随着一些步骤的退出,您应该深入了解这一点。
Assuming you know (or suspect) the function where the infite loop happens you could add code like this:
假设您知道(或怀疑)发生无限循环的函数,您可以添加如下代码:
var calls = 0;
function iSuspectToBeLoopingInfititely() {
calls += 1;
if (calls > 100) { debugger; }
}
This will stop the JavaScript debugger in Chrome once the method has been called 100 times.
Note: Chrome will only break for debugger;
calls if you actually have the Developer Tools window open.
一旦该方法被调用 100 次,这将停止 Chrome 中的 JavaScript 调试器。注意:debugger;
如果您确实打开了“开发者工具”窗口,Chrome 只会中断通话。
回答by crashbus
Found another way of debugging. In my case the error was caught and so no errors where logged to the console. Found the bug with the checkbox Pause on caught exceptions
. You find the option in den dev tools unter the Sources
tab. To show and enable the checkbox click on the last icon:
找到了另一种调试方式。在我的情况下,错误被捕获,因此没有错误记录到控制台。使用复选框发现错误Pause on caught exceptions
。您可以在选项Sources
卡下的den dev 工具中找到该选项。要显示和启用复选框,请单击最后一个图标:
After enabling this, the debugger pauses on every caught exception.
启用此功能后,调试器会在每个捕获的异常上暂停。
回答by armyofda12mnkeys
I had issues in Chrome, I would see in the browser window 'Paused in debugger' but couldn't see where as maybe Chrome was confused since it was in a loop ... In Firefox, it recognized its taking too long and then a popup comes up after 30seconds to 1minute saying the file and general line # its frozen on which helps out to debug further and set Breakpoints around that area.
我在 Chrome 中遇到了问题,我会在浏览器窗口中看到“在调试器中暂停”,但看不到 Chrome 可能混淆的地方,因为它处于循环中......在 Firefox 中,它认识到它花费的时间太长,然后30 秒到 1 分钟后弹出窗口说文件和一般行 # 它被冻结,这有助于进一步调试并在该区域周围设置断点。