How to terminate script execution when debugging in Google Chrome?

后端 未结 11 1145
傲寒
傲寒 2020-12-07 08:16

When stepping through JavaScript code in Google Chrome debugger, how do I terminate script execution if I do not want to continue? The only way I found is closing the browse

相关标签:
11条回答
  • 2020-12-07 08:55

    Open the source tab in 'Developer Tools', click on a line number in a script that is running, this will create a breakpoint and the debugger will break there.

    0 讨论(0)
  • 2020-12-07 08:56

    2020 April update

    As of Chrome 80, none of the current answers work. There is no visible "Pause" button - you need to long-click the "Play" button to access the Stop icon:

    0 讨论(0)
  • 2020-12-07 09:02

    One way you can do it is pause the script, look at what code follows where you are currently stopped, e.g.:

    var something = somethingElse.blah;
    

    In the console, do the following:

    delete somethingElse;
    

    Then play the script: it will cause a fatal error when it tries to access somethingElse, and the script will die. Voila, you've terminated the script.

    EDIT: Originally, I deleted a variable. That's not good enough. You have to delete a function or an object of which JavaScript attempts to access a property.

    0 讨论(0)
  • 2020-12-07 09:03

    In Chrome, there is "Task Manager", accessible via Shift+ESC or through

    Menu → More Tools → Task Manager

    You can select your page task and end it by pressing "End Process" button.

    0 讨论(0)
  • 2020-12-07 09:03

    Good question here. I think you cannot terminate the script execution. Although I have never looked for it, I have been using the chrome debugger for quite a long time at work. I usually set breakpoints in my javascript code and then I debug the portion of code I'm interested in. When I finish debugging that code, I usually just run the rest of the program or refresh the browser.

    If you want to prevent the rest of the script from being executed (e.g. due to AJAX calls that are going to be made) the only thing you can do is to remove that code in the console on-the-fly, thus preventing those calls from being executed, then you could execute the remaining code without problems.

    I hope this helps!

    P.S: I tried to find out an option for terminating the execution in some tutorials / guides like the following ones, but couldn't find it. As I said before, probably there is no such option.

    http://www.codeproject.com/Articles/273129/Beginner-Guide-to-Page-and-Script-Debugging-with-C

    http://www.nsbasic.com/app/tutorials/TT10.htm

    0 讨论(0)
  • 2020-12-07 09:05

    If you are encountering this while using the debugger statement,

    debugger;
    

    ... then I think the page will continue running forever until the js runtime yields, or the next break. Assuming you're in break-on-error mode (the pause-icon toggle), you can ensure a break happens by instead doing something like:

    debugger;throw 1;
    

    or maybe call a non-existent function:

    debugger;z();
    

    (Of course this doesn't help if you are trying to step through functions, though perhaps you could dynamically add in a throw 1 or z() or somesuch in the Sources panel, ctrl-S to save, and then ctrl-R to refresh... this may however skip one breakpoint, but may work if you're in a loop.)

    If you are doing a loop and expect to trigger the debugger statement again, you could just type throw 1 instead.

    throw 1;
    

    Then when you hit ctrl-R, the next throw will be hit, and the page will refresh.

    (tested with Chrome v38, circa Apr 2017)

    0 讨论(0)
提交回复
热议问题