Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Technique to detect where Javascript gets stuck?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading