JavaScript: 如何在 Chrome 调试工具中设置条件断点

我有一个简单的 js 文件,它可以连续打印日期。

我正在使用谷歌 Chrome 调试工具(F12)

我的问题是 有没有可能在谷歌浏览器中设置一个条件断点?

在我的代码中,如果秒值等于50,我想设置一个断点?

s = date.getSeconds();

这个 是我的源文件所在的 jsfiddle

(不知道为什么它在 jsfiddle 中不工作)

无论如何,我的问题是,有没有可能设置一个铬调试工具的条件断点?

36606 次浏览

Yes, it is possible.

Right click the marker of the breakpoint and select "Edit breakpoint..." there you can set the condition.

From Chrome Developer Tools on Breakpoints at developers.google.com (Emphasis mine):

Note: All the breakpoints you have set appear under Breakpoints in the right-hand sidebar. Clicking on the entry jumps to the highlighted line in the source file. Once you have a breakpoint set, right click on the blue tag breakpoint indicator to set a conditional statement for that breakpoint. Type an expression and the breakpoint will only pause only if the condition is true.

Take a look at debugger statement. Basically it invokes any debugger tools available, and in Chrome it acts as if interpreter met a breakpoint.

Your code would be:

s = date.getSeconds();
if (s == 50) {
debugger;
}

From reference:

[debugger] Invokes any available debugging functionality. If no debugging functionality is available, this statement has no effect.

You can set a conditional break point in Google Chrome, by following these steps:

1.right click the breakpoint where u want to stop,please chk on enter image description here

2.click "Add conditional breakpoint",one text will be appear,there u can add condition (the result will be 'true' if the condition satisfied ,else 'false'), the breakpoint color will goto orange after the condition added,chk on enter image description here

3.reload same page u can see the breakpoint will work if the condition is satisfied like enter image description here