如何从 Chrome 中的代码设置 JavaScript 断点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10050465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:38:36  来源:igfitidea点击:

How to set a JavaScript breakpoint from code in Chrome?

javascriptdebugginggoogle-chromebreakpoints

提问by Paul

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().

我想强制 Chrome 调试器通过代码在一行中断,或者使用某种注释标记,例如console.break().

回答by xn.

You can use debugger;within your code. If the developer console is open, execution will break. It works in firebug as well.

您可以debugger;在代码中使用。如果开发者控制台打开,执行将中断。它也适用于萤火虫。

回答by Protector one

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

您还可以使用debug(function), 在function被调用时中断。

Command Line API Reference: debug

命令行 API 参考:调试

回答by Andrija

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/

正如其他人已经说过的那样,debugger;是要走的路。我写了一个小脚本,你可以在浏览器的命令行中使用它在函数调用之前设置和删除断点:http: //andrijac.github.io/blog/2014/01/31/javascript-breakpoint/

回答by Florian Margaine

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

chrome断点截图

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

然后,您将能够在右侧选项卡中跟踪断点(如屏幕截图所示)。

回答by user2539341

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.

这是可能的,您可能想要这样做的原因有很多。例如,在接近页面加载开始时调试 javascript 无限循环,这会阻止 chrome 开发人员工具集(或 firebug)正确加载。

See section 2 of

见第 2 节

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

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.

或者只是在所需的测试点将包含调试器一词的行添加到您的代码中。

回答by Parth Raval

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.

断点:-

断点将停止执行,并让您检查 JavaScript 值。

检查值后,您可以恢复代码的执行(通常使用播放按钮)。

调试器:-

调试器;停止 JavaScript 的执行,并调用调试函数。

调试器语句暂停执行,但它不会关闭任何文件或清除任何变量。

Example:-
function checkBuggyStuff() {
  debugger; // do buggy stuff to examine.
};

回答by Lance Pollard

回答by Keet Sugathadasa

There are many ways to debug JavaScript code. Following two approaches are widely used to debug JavaScript via code

有很多方法可以调试 JavaScript 代码。以下两种方法被广泛用于通过代码调试 JavaScript

  1. Using console.log()to print out the values in the browser console. (This will help you understand the values at certain points of your code)

  2. Debugger keyword. Add debugger;to the locations you want to debug, and open the browser's developer console and navigate to the sources tab.

  1. 使用console.log()浏览器控制台打印出来的值。(这将帮助您了解代码某些点的值)

  2. 调试器关键字。添加debugger;到要调试的位置,然后打开浏览器的开发人员控制台并导航到源选项卡。

For more tools and ways in which you debug JavaScript Code, are given in this link by W3School.

有关调试 JavaScript 代码的更多工具和方法,请在W3School 的此链接中提供

回答by S To

I wouldn't recommend debugger;if you just want to kill and stop the javascript code, since debugger;will just temporally freeze your javascript code and not stop it permanently.

debugger;如果您只想终止并停止 javascript 代码,我不建议您这样做,因为它debugger;只会暂时冻结您的 javascript 代码,而不会永久停止。

If you want to properly kill and stop javascript code at your command use the following:

如果您想在您的命令中正确终止和停止 javascript 代码,请使用以下命令:

throw new Error("This error message appears because I placed it");

throw new Error("This error message appears because I placed it");