Hey.

There are situations when, during the execution of a script, the browser hangs along with its devtuls. At the same time, I cannot find out where the error is in the script, since the devtusl is frozen. You have to comment out different pieces of code and catch the place with an error in this way.

For example, at the moment I am debugging a script, the script causes ANY browser to hang. And any browser devtuls doesn't work.

What can be done?

Already thought of the answer. A browser hang causes not infinite recursion (it causes an error — an overflow of the execution context stack, not a browser hang), but an infinite loop. To catch DIRECTLY which of the dozens of cycles in the program looped endlessly, you can use, for example, a recursive function instead of cycles. For example, instead of a loop

for(var i=0;i<5;i=i+1) { console.log(i); } 

Write recursive function

 var i=0; function cycle(){ console.log(i); i=i+1; if(i==5) return; cycle() }; cycle(); 
  • Comments are not intended for extended discussion; conversation moved to chat . - Nicolas Chabanovsky

1 answer 1

Unfortunately, script debuggers in some modern browsers are too sensitive to some errors in the code being debugged.

For example, the following script completely kills Chrome 52 and Edge 20.

 var j=0; function cycle(){ if (j > 5) return; for (var i = 0; i<100000; i++) { j = j + 1; cycle(); j = j - 1; } }; cycle(); 

Under Windows, there are two solutions to the problem:

  • Take the good old Firefox. After a couple of seconds of execution, he realizes that something has gone wrong, and offers to stop and debug the script.
  • Take IE + external debugger (Visual Studio). Enable the debugging of scripts in IE settings, reproduce the problem situation, cling to the process by the studio and stop the execution.