简体   繁体   English

chrome:如何中止在`debugger`语句中停止的脚本?

[英]chrome: How to abort a script that is stopped at `debugger` statement?

Consider the following silly example: 考虑以下愚蠢的示例:

<!doctype html>
<meta charset="utf-8">
<title>dumb snippet</title>
<script>
var i = 0;
while (i < 100) {
    console.log(i);
    debugger;
    i += 1;
}
</script>

If I run this code using Google Chrome's DevTools, the debugger dutifully stops execution at the debugger statement, but I have not found any way to immediately abort (and restart) the script. 如果我使用Google Chrome的DevTools运行此代码,则调试器会在debugger语句中忠实地停止执行,但是我没有找到立即终止(并重新启动)脚本的任何方法。 AFAICT, pressing Ctrl-R or even Shift-Ctrl-R , rather than reloading the page, merely causes the execution to continue. AFAICT,按Ctrl-R甚至Shift-Ctrl-R而不是重新加载页面,只会导致执行继续。

The only recourse I've found is obvious, but unnecessarily inconvenient: kill the tab/window altogether, and open up a new one. 我发现的唯一方法是显而易见的,但是不必要地不方便:完全杀死选项卡/窗口,然后打开一个新的选项卡/窗口。

Does the Google Chrome DevTools provide some way to immediately abort a script that is stopped at a debugger statement? Google Chrome DevTools是否提供某种方法来立即中止在debugger语句中停止的脚本?

(If the answer happens to be "no", please do not post work-arounds. I can think of plenty of them, eg holding F-8 until the loop exits, although this won't work, of course, if the loop turns out to be an infinite loop. In any case, here I am only interested in whether there is an "official" way to abort and restart such a script.) (如果答案碰巧是“否”,请不要发布解决方法。我可以想到很多解决方法,例如,按住F-8直到循环退出,尽管这样当然行不通,事实证明这是一个无限循环。无论如何,在这里,我只关心是否有一种“官方”方法来中止并重新启动此类脚本。)

You can do it, but you must prepare your code first. 您可以做到,但必须先准备好代码。

Instructions for halting script execution in Google Chrome Dev Tools: 在Google Chrome开发者工具中暂停脚本执行的说明:

(1) Create a global variable: (1)创建一个全局变量:

var devquit=0;
$(document).ready({
    //the rest of your code

(2) Any place where you may wish to quit, insert a test of this variable: (2)在您可能希望退出的任何地方,插入对此变量的测试:

//Lotsa code
if (devquit > 0) return false;

(3) Pause execution of script on-or-before the above test line(s) (3)在上述测试行之前或之前暂停脚本的执行

(4) Switch to console (4)切换到控制台

(5) Type: (5)类型:

> devquit
0
> devquit=1   <=== only this line is necessary
> devquit
1

(6) Continue script execution. (6)继续执行脚本。 Script will return false when it executes the test from step (2) above 脚本从上面的步骤(2)执行测试时将return false


Notes: 笔记:

(A) This trick works with global variables and objects, but it will not work with local variables. (A)此技巧适用于全局变量和对象,但不适用于局部变量。

(B) So, you can still use this trick with already-running code IF you have either a global variable or an object that will return false if it has a given value. (B)因此, 如果您具有全局变量或对象(如果具有给定值则返回false),则仍可以对已经运行的代码使用此技巧。

(C) In a pinch, you can also delete an element from the DOM (on the elements tab) that will cause a javascript error. (C)在紧要关头,您还可以从DOM中删除元素(在元素选项卡上),这将导致JavaScript错误。 For example, suppose you have code var cartype = $('#cartype').val(); 例如,假设您有代码var cartype = $('#cartype').val(); If you delete the element with ID= cartype before that line of code, then the js will break on that line. 如果在该行代码之前删除ID = cartype的元素,则js将在该行中断。 However, the element will still be missing when you try to re-run the code. 但是,当您尝试重新运行代码时,该元素仍然会丢失。 The trick described above allows you to run and re-run the code ad infinitum. 上述技巧可让您无限次地运行和重新运行代码


More notes: 更多说明:

(a) Insert breakpoint into code: just type debugger; (a)在代码中插入断点:只需键入debugger; on a line by itself. 一条线上。 If DevTools is open, the script will jump into debugger at that point. 如果打开DevTools,该脚本将在此时跳入调试器。 If DevTools not open, code will ignore statement. 如果未打开DevTools,则代码将忽略语句。

(b) Want to avoid jumping into the jQuery library when debugging code? (b)想要避免在调试代码时跳入jQuery库吗? Blackbox it. 黑匣子吧。 See blackbox instructions for Chrome - or - for Firefox 查看Chrome的黑盒说明 -或-Firefox


Gratitude (please visit and upvote): 感激(请访问并投票):

Javascript Debugging line by line using Google Chrome 使用Google Chrome浏览器逐行进行JavaScript调试

Is it possible to change javascript variable values while debugging in Google Chrome? 在Google Chrome浏览器中调试时是否可以更改javascript变量值?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM