How to set a JavaScript breakpoint from code in Ch

2019-01-02 16:35发布

I want to force the Chrome debugger to break on a line via code, or else using some sort of comment tag such as something like console.break().

10条回答
无色无味的生活
2楼-- · 2019-01-02 16:55

On the "Scripts" tab, go to where your code is. At the left of the line number, click. This will set a breakpoint.

Screenshot:

screenshot of breakpoint in chrome

You will then be able to track your breakpoints within the right tab (as shown in the screenshot).

查看更多
明月照影归
3楼-- · 2019-01-02 16:56
皆成旧梦
4楼-- · 2019-01-02 17:05

You can also use debug(function), to break when function is called.

Command Line API Reference: debug

查看更多
看淡一切
5楼-- · 2019-01-02 17:05

Breakpoint :-

breakpoint will stop executing, and let you examine JavaScript values.

After examining values, you can resume the execution of code (typically with a play button).

Debugger :-

The debugger; stops the execution of JavaScript, and callsthe debugging function.

The debugger statement suspends execution, but it does not close any files or clear any variables.

Example:-
function checkBuggyStuff() {
  debugger; // do buggy stuff to examine.
};
查看更多
有味是清欢
6楼-- · 2019-01-02 17:06

As other have already said, debugger; is the way to go. I wrote a small script that you can use from the command line in a browser to set and remove breakpoint right before function call: http://andrijac.github.io/blog/2014/01/31/javascript-breakpoint/

查看更多
情到深处是孤独
7楼-- · 2019-01-02 17:07

It is possible and there are many reasons you might want to do this. For example debugging a javascript infinite loop close to the start of the page loading, that stops the chrome developer toolset (or firebug) from loading correctly.

See section 2 of

http://www.laurencegellert.com/2012/05/the-three-ways-of-setting-breakpoints-in-javascript/

or just add a line containing the word debugger to your code at the required test point.

查看更多
登录 后发表回答