使用 WebInspector 的 Javascript 调试器,如何摆脱无限循环?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不熟悉 WebKit,但这听起来像是一个常见问题,我通常调试如下:在循环范围之外声明一个整数,并为每次迭代递增它,然后在迭代计数超过时抛出异常最大预期可能的迭代次数。因此,在伪代码中,可以使用类似以下内容来调试此问题:
我不认为这足够具体以保证得到可接受的答案,但我希望它可以帮助您解决问题。
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:
I don't expect that this is specific enough to warrant an accepted answer, but I hope it helps you solve your problem.
您尝试过使用 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
在使用 Firefox(我知道这个问题没有指定 Firefox,但我只是把它扔进去,以防它对某人有帮助)、Safari 和 Chrome 时,我发现没有一致的方法来闯入无限循环,但是如果需要的话,有一些方法可以设置执行以中断循环。请参阅下面我的测试代码。
Firefox
简直就是垃圾。它只是站在那里,旋转着彩虹。我不得不杀掉它。
Safari
Safari 的好处是它最终会弹出一个对话框,询问您是否要停止。你应该说是的。然后点击
command-option i
来调出Web Inspector。源应该会弹出消息,指出 Javascript 超出了超时时间。点击右侧顶部的暂停按钮,然后刷新。代码将重新加载,现在逐步执行:我喜欢使用command-;
因为它会逐步执行每个调用。如果您的代码很复杂,您可能永远无法读完。但不要点击继续
(command-/
),否则您将回到第一个方块。Chrome
三者中最有用的。如果您天真地加载代码,它将永远继续下去。但是,在加载页面之前,请打开 Web 检查器并选择“始终加载资源”。然后重新加载页面。当页面尝试加载时,您可以单击脚本并在 Javascript 运行时暂停它。这就是我一直在寻找的。
代码
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 usingcommand-;
because it steps through every call. If you have complex code you might never get to the end. Don't hitcontinue
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
您是否尝试过
top
查看进程树?找到该程序的 PID,然后输入kill -9 [PID]
。Have you tried
top
to look at the process tree? Find the PID of the program and then typekill -9 [PID]
.