使用 WebInspector 的 Javascript 调试器,如何摆脱无限循环?

发布于 2024-10-08 11:20:38 字数 225 浏览 7 评论 0原文

我在 JavaScript 代码中编写了一个无限循环。使用WebKit的Web Inspector,如何终止执行?我必须退出 Safari 并再次重新打开它(当然是在更改我的代码之后)。

编辑:更具体地说,我正在寻找一种方法来进入循环执行进程/线程以查看为什么循环没有终止。类比是在 GDB 中,我可以执行 ^C 并中断进程。我并不是在寻找一种方法来杀死我的网络浏览器。我已经很擅长了。

I wrote an infinite loop in my javascript code. Using WebKit's Web Inspector, how do I terminate execution? I have to quit Safari and reopen it again (after changing my code of course).

EDIT: To be more specific, I'm looking for a way to enter the looping executing process/thread to see why the loop isn't terminating. An analogy would be in GDB where I could do a ^C and break into the process. I'm not looking for a way to kill my web browser. I'm pretty good at that already.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忆沫 2024-10-15 11:20:38

我不熟悉 WebKit,但这听起来像是一个常见问题,我通常调试如下:在循环范围之外声明一个整数,并为每次迭代递增它,然后在迭代计数超过时抛出异常最大预期可能的迭代次数。因此,在伪代码中,可以使用类似以下内容来调试此问题:

var iterations = 0;
var greatestPossibleNumberOfValidIterations = 500;
while(shouldLoopBooleanTest){
  iterations++;
  if(iterations>greatestPossibleNumberOfValidIterations){
     //do debugging/error handling
  }
} 

我不认为这足够具体以保证得到可接受的答案,但我希望它可以帮助您解决问题。

I'm not familiar with WebKit, but this sounds like a common problem that I usually debug as follows: Declare an integer outside of the scope of the loop, and increment it for each iteration, then throw an exception when the iteration count exceeds the maximum expected possible amount of iterations. So, in pseudo-code, something like the following could be used to debug this problem:

var iterations = 0;
var greatestPossibleNumberOfValidIterations = 500;
while(shouldLoopBooleanTest){
  iterations++;
  if(iterations>greatestPossibleNumberOfValidIterations){
     //do debugging/error handling
  }
} 

I don't expect that this is specific enough to warrant an accepted answer, but I hope it helps you solve your problem.

玻璃人 2024-10-15 11:20:38

您尝试过使用 F11 吗?这是进入的关键: https://trac.webkit.org/wiki/WebInspector

Have you tried using F11? This is the key for step into: https://trac.webkit.org/wiki/WebInspector

网白 2024-10-15 11:20:38

在使用 Firefox(我知道这个问题没有指定 Firefox,但我只是把它扔进去,以防它对某人有帮助)、Safari 和 Chrome 时,我发现没有一致的方法来闯入无限循环,但是如果需要的话,有一些方法可以设置执行以中断循环。请参阅下面我的测试代码。

Firefox

简直就是垃圾。它只是站在那里,旋转着彩虹。我不得不杀掉它。

Safari

Safari 的好处是它最终会弹出一个对话框,询问您是否要停止。你应该说是的。然后点击command-option i来调出Web Inspector。源应该会弹出消息,指出 Javascript 超出了超时时间。点击右侧顶部的暂停按钮,然后刷新。代码将重新加载,现在逐步执行:我喜欢使用 command-; 因为它会逐步执行每个调用。如果您的代码很复杂,您可能永远无法读完。但不要点击继续 (command-/),否则您将回到第一个方块。

Chrome

三者中最有用的。如果您天真地加载代码,它将永远继续下去。但是,在加载页面之前,请打开 Web 检查器并选择“始终加载资源”。然后重新加载页面。当页面尝试加载时,您可以单击脚本并在 Javascript 运行时暂停它。这就是我一直在寻找的。

代码

<html>
  <head></head>
  <body onload="TEST.forever.loop()">
    Nothing
  </body>
  <script type="text/javascript">
    TEST = {};
    TEST.forever = {
      loop : function() {
        var i = 0;
        while (i >= 0) {
          i++;
        }
      }
    };
  </script>
</html>

In using Firefox (I know the question doesn't specify Firefox, but I'm just throwing this in, in case it helps somebody), Safari, and Chrome, I found that there isn't a consistent way to break into an infinite loop, but there are ways to setup execution to break into a loop if you need to. See my test code below.

Firefox

Utter trash. It just stood there spinning it's rainbow. I had to kill it.

Safari

The nice thing about Safari is that it will eventually throw up a dialog asking if you want to stop. You should say yes. Then hit command-option i to bring up the Web Inspector. The source should pop-up saying that the Javascript exceeded the timeout. Hit the pause button in the right hand side towards the top, then refresh. The code will reload, now step through: I like using command-; because it steps through every call. If you have complex code you might never get to the end. Don't hit continue though (command-/) or you'll be back at square one.

Chrome

The most useful of the three. If you naively load the code, it will keep going forever. But, before loading the page, open the Web Inspector and select 'Load resources all the time'. Then reload the page. While the page is trying to load, you can click over to scripts and pause the Javascript while it is running. This is what I was looking for.

Code

<html>
  <head></head>
  <body onload="TEST.forever.loop()">
    Nothing
  </body>
  <script type="text/javascript">
    TEST = {};
    TEST.forever = {
      loop : function() {
        var i = 0;
        while (i >= 0) {
          i++;
        }
      }
    };
  </script>
</html>
剧终人散尽 2024-10-15 11:20:38

您是否尝试过 top 查看进程树?找到该程序的 PID,然后输入 kill -9 [PID]

Have you tried top to look at the process tree? Find the PID of the program and then type kill -9 [PID].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文