当JavaScript进入无限循环和递归调用时如何调试javascript? [英] How to debug javascript when it goes into infinite loops and recursive calls in Javascript?

查看:92
本文介绍了当JavaScript进入无限循环和递归调用时如何调试javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您处于无限循环或递归调用时,基本上浏览器停止响应任何内容(在Chrome或FF上)。您看不到日志,无法进入调试器,即使您无法打开控制台本身。浏览器只是冻结。这太烦人了似乎我只能坐在这里抓我的头......任何人都可以揭示如何解决这个问题?

解决方案

您可以尝试的另一个伎俩是让Chrome中的Web开发人员工具打开,并在浏览器显示挂起时尝试​​点击暂停。那么它应该在当前正在执行的行中断。有一些步骤,你应该得到这个的底部。



假设你知道(或怀疑)这个函数的位置,你可以添加如下代码: / p>

  var calls = 0; 
函数iSuspectToBeLoopingInfititely(){
调用+ = 1;
if(calls> 100){debugger;
}

一旦方法被调用,这将阻止Chrome中的JavaScript调试器100次
注意:如果您真的打开开发人员工具窗口,Chrome将只会中断调试器; 调用。


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?

解决方案

Another trick you could try is to have the Web developer tools in Chrome open and try to hit Pause when 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.

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.

这篇关于当JavaScript进入无限循环和递归调用时如何调试javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆