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 loopi开发者_运维问答ng 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.
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.
Have you tried top
to look at the process tree? Find the PID of the program and then type kill -9 [PID]
.
Have you tried using F11? This is the key for step into: https://trac.webkit.org/wiki/WebInspector
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>
精彩评论