Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you interrupt JS in browser?

What is the equivalent of pressing Ctrl+C in console, for Chrome and Firefox? While implementing various algorithms I often write some buggy (while) loop, in Javascript, that doesn't exit, makes the browser freeze. Reloading doesn't work, clicking little X for closing the tab doesn't do anything, and in a while (literally) I'm outta memory, system is swapping, and I'm leaving for a coffee break.

like image 727
skrat Avatar asked Mar 11 '12 22:03

skrat


1 Answers

In Chrome, you can hit 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 closing the tab would not.

The caveat is, sometimes Chrome will streamline several tabs into one process, and this will kill all the tabs associated with the process.


Another approach you can take to avoid while loops hanging the 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 Chrome's task manager and select the item on the bottom of the context menu to reveal a strange easter egg.

like image 143
Dagg Nabbit Avatar answered Oct 20 '22 19:10

Dagg Nabbit