How do you stop an infinite loop in Javascript?

Javascript

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

2018 update:

In Chrome 67, if you have the DevTools open (F12), you can end the infinite loop without killing the whole tab:

  • Go to the Sources panel and click "Pause script execution".
  • Hold that same button and now select the "Stop" icon.

enter image description here

https://developers.google.com/web/updates/2018/04/devtools#stop

Solution 2 - Javascript

I'm using Chome Version 45.0.2454.101

At the top right hand corner, at the hamburger menu, click on More Tools > Task Manager, and then kill the tab from there. This is if attempting to close the tab fails. Otherwise a simple 'X' on the tab kills it.

Solution 3 - Javascript

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.

Solution 4 - Javascript

If you are using Chrome you can easily kill not responding tab:

  1. Switch to any other tab
  2. Press Shift + Esc to open Chrome's Task Manager
  3. Find your tab in the list (Should be the most memory consumer)
  4. Click End Process

Important: Navigating to another tab is important, because if you are on the original tab JavaScript will make the browser unable to process your key press and do nothing because of its infinite loop.

Solution 5 - Javascript

ref: https://superuser.com/questions/92617/stop-never-ending-popup-alerts-in-firefox
Firefox is particularly problematic ...

Warning! Caveat! Do NOT run this!

javascript:
    while (true) alert("irritated and exhausted - yet?");

This will go "infinite" and will NOT exhaust an internal timeout since the script will not chew up CPU time fast enough. In FF 11 this guarantees there will be no "unresponsive script" abortion opportunities.

Gracefully stopping just the offending script was possible and trivial in early browser versions, using manual intervention, without croaking and aborting the whole browser. To not have such control is a major browser software design flaw. Unreasonable dexterity and reflex are required to effect the manual motor mechanics of the "solutions" described in the reference.

Caveat: It is possible for scripts to go "infinite" w/o timing out AND w/o alert type prompts. These are particularly pernicious and annoying. Basically, the scripts run slowly enough so that the CPU time cycle allotment of say 20 sec. is stretched out over several minutes or hours or ..., before timing out, by suspension of execution pending resumption on an event trigger. Instead of timing CPU cycles it would be far better for scripts to timeout on real world clocking. (Ever notice how you cannot abort a script that is trying to retrieve content - but unsuccessfully - for constructing a page? In FF both the address bar buttons reload and cancel/stop are disabled though the tab at least can be closed.) Normal javascript Timeout() and setInterval() calls do not suffer from this and are conditioned so that while suspended, manual intervention is possible to abort them "gracefully".

Test environment for empirical observations:

window.navigator.userAgent=
Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:11.0) Gecko/20100101 Firefox/11.0

PS. the script

for (i=1; i > 0; ++i);

will eventually trap on an overflow error when i exceeds the maximum value allowed.

Solution 6 - Javascript

Depends on the browser. Some let you click the "stop" button to stop javascript execution. Others don't.

I suggest the best way is to just kill the browser or tab entirely.

Solution 7 - Javascript

Most browsers have a "slow script performance" warning that comes up when an out of control javascript is taking a very long time to execute. This warning dialog usually gives the option to kill the offending script.

Solution 8 - Javascript

In Firefox

Go to Developer Tools > Debugger, and click pause button.

enter image description here

In Chrome

Go to Developer Tools > Sources, and click pause button.

enter image description here

Solution 9 - Javascript

most decent browsers do show an "unresponsive script" warning... If not, I guess your best course of action would be to find out why the warning is not popping up.

Solution 10 - Javascript

From https://superuser.com/questions/92617/stop-never-ending-popup-alerts-in-firefox#comment93927_92617, by anonymous:

>This isn't a satisfactory general-purpose solution, but with Greasmonkey (or maybe Ubiquity or Jetpack) you could overwrite window.alert with a function that calls window.confirm and optionally throw(s) an error, stopping all script execution, or toggles a flag to stop alerts. That might be useful if a site you keep going back to presents this behavior.

Solution 11 - Javascript

What I do when using Chrome and this happens is hitting shift+ctrl+esc to bring up the windows taskmanager. Then switch to the processes-tab and scroll through the chrome.exe processes(chrome has one process for each open tab) till I find one with significantly higher cpu usage than the oters. It tends to have around 30% for me while all others have like 0-2% Then I'll just end that process. Actually the same can be done by going to tools>taskmanager or shift+esc in chrome to open its custom taskmanager for its processes. Might be easier to use that since it shows more info of the tabs.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnickfView Question on Stackoverflow
Solution 1 - JavascriptSphinxxxView Answer on Stackoverflow
Solution 2 - JavascriptSevin LimView Answer on Stackoverflow
Solution 3 - JavascriptalexView Answer on Stackoverflow
Solution 4 - JavascriptRahmat AliView Answer on Stackoverflow
Solution 5 - JavascriptloopedView Answer on Stackoverflow
Solution 6 - JavascriptRandolphoView Answer on Stackoverflow
Solution 7 - JavascriptSoviutView Answer on Stackoverflow
Solution 8 - JavascriptDevonDahonView Answer on Stackoverflow
Solution 9 - JavascriptjrharshathView Answer on Stackoverflow
Solution 10 - Javascriptuser66001View Answer on Stackoverflow
Solution 11 - JavascriptCloxView Answer on Stackoverflow