Skip to content
Advertisement

How do you stop an infinite loop in Javascript?

Let’s say I accidentally wrote this:

 do { } while (true);

…and then ran it. Apart from killing your browser, is there a way to stop javascript execution (the equivalent of Ctrl+Break in basic, or Ctrl+C)?

Normally, after about 30 seconds your browser asks if you want to stop the long-running script, but this doesn’t always happen (as I just found out)!

FYI: A simple loop such as this: for (i=1; i > 0; ++i); will cause my browser to crash (Firefox 3.5b4). I don’t feel much like testing to see if it’s any of my add-ons. Continuously restarting my browser isn’t my idea of a fun Monday night.

Advertisement

Answer

At least with Chrome, you may be able to kill off the individual tab and not the whole application.

Randolpho has also informed me that IE8 has similar functionality.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement