Home > Back-end >  Technique to detect where Javascript gets stuck?
Technique to detect where Javascript gets stuck?

Time:11-11

I'm debugging unobfuscated Javascript code. At some point, the code gets stuck in some never-returning function or infinite loop. What's the fastest way to find this part of the code? I assume there's a better way than adding a bunch of breakpoins/prints.

I'm using Chrome Developer Tools.

CodePudding user response:

If you open devtools and wait for the code to get to the infinite loop, you can click the pause button in devtools, which will pause the code somewhere within the loop.

enter image description here

For instance, I wrote a page with this code:

setTimeout(() => {
    let n = 0;
    while (isFinite(n)) {
        n = n   1;
    }
}, 800);

and ran it, opened devtools, and clicked the Pause button. It paused the code in the while:

enter image description here

CodePudding user response:

If you are really on a loop with the console opened, go to "Sources" tab, then click on the "Pause" icon. The code will pause and you will see where it is stuck :)

  • Related