How do you interrupt JS in a browser?

What is equivalent to pressing Ctrl + C on the console for Chrome and Firefox? When implementing various algorithms, I often write some kind of buggy ( while ), in Javascript, which does not work out, it makes the browser freeze. Rebooting does not work, clicking little X to close the tab does nothing, and after a while (literally) I have memory, the system swaps, and I leave for a coffee break.

+8
javascript browser firefox google-chrome
source share
4 answers

For JavaScript, there is no Ctrl + C JavaScript browsers typically protect themselves. If any JavaScript freezes, they will invoke a dialog asking if the user wants to stop JavaScript.

The timeout duration can usually be found in the browser settings. You can find how to do it for FireFox here: http://www.trixya.net/index.php/internet/how-to-set-javascript-timeout-in-firefox

+5
source share

In Chrome, you can press Shift+ESC (or right-click the title bar and open the Chrome task manager) and kill the process associated with the hung tab. This will work in cases where the tab will not close.

There is a caveat, sometimes Chrome optimizes several tabs in one process, and this will kill all the tabs associated with the process.


Another approach that you can take to avoid while loops that hang on your browser is to write code like this (you can take it out after testing):

 var maxIterations = 100000; while (foo) { if (!maxIterations--) throw new Error('Max iterations hit, aborting.'); // do stuff } 

Right-click in the Chrome Task Manager and select the item at the bottom of the context menu to open the weird Easter Egg.

+5
source share

From the little understanding that I have in your ways of working, how I will continue:

  • Run the script only event, for example, a button click. This will prevent the onload script from running.
  • Chrome allows you to set breakpoints in js code on the developer tool scripts tab
+1
source share

There is no real "breaker" for running Javascript code in a browser. ECMAscript is executed in the so-called "user interface thread", which means that all visualization tools occur in the same queue in which the ECMAscript code is executed.

This, in turn, means that the endless loop in ECMAscript automatically freezes in the interaction of all browsers.

The only way to avoid this is to write clean, clean code. If this is the case, most browsers realize that the user interface thread is too busy and asks the user if he wants to cancel running javascript processes. If you do not want to wait for this, your only choice is to kill the entire browser / tab process.

However, if you know that some part of your script can cause an endless loop, you can either install breakpoints manually in some developer tools or insert the debugger; keyword debugger; straight to your script. This causes the javascript interpreter to pause in the current line, and you have the option to parse the following code (while the conditions for the example) and cancel execution if it looks bad.

0
source share

All Articles