86

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.

9
  • Out of curiousity, were you able to determine what caused your browser not to throw a 'slow script' warning?
    – alex
    May 25, 2009 at 4:47
  • 3
    no clue! (this 15 character limit is annoying)
    – nickf
    May 25, 2009 at 4:59
  • Does this belong in SuperUser?
    – Chetan
    Apr 24, 2011 at 19:42
  • 3
    @Chetan: It's pretty much entirely programming related...
    – nickf
    Apr 24, 2011 at 20:03
  • Oh, I misunderstood this as a question about browser usage, rather than about plugin development. My bad.
    – Chetan
    Apr 24, 2011 at 20:45

12 Answers 12

71

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

3
  • This didn't work for me, though possibly with all the stuff in sources, I just can't find the right file.
    – TiggerToo
    May 28, 2019 at 19:30
  • @TiggerToo - You don't need to find the right file first. Just click the pause button, and the browser will stop wherever it's stuck.
    – Sphinxxx
    Dec 26, 2019 at 21:07
  • This doesn't work. F12 is unresponsive. Manually opening devtools shows a 'blank' window. Pause button only, no stop, and it does nothing. Only possible to kill process.
    – bryc
    Jul 18, 2022 at 13:51
64

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.

1
  • having a endless loop in a Ajax callback causes the plain X to fail when you have devtools open.
    – masterX244
    Feb 23, 2017 at 8:37
24

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.

5
  • 3
    Can you elaborate on how to kill of a single tab? Do we look for the right chrome.exe and kill it in the task manager?
    – mellamokb
    Apr 26, 2011 at 1:30
  • 1
    negatory, does not work! at least not on Version 36.0.1985.125 for linux
    – Octopus
    Oct 11, 2014 at 4:56
  • 2
    It worked for me, in Version 40.0.2214.115 (64-bit), but I had to wait several seconds. This answer would be 10× as useful, and up-voteable, if it included these details. Mar 4, 2015 at 4:24
  • 1
    I found this worked for me however if you press shift+esc in the same tab that has the infinite loop Chrome will give javascript a chance to process the keypress/keydown/keyup before deciding if it should do anything however since your javascript is busy and cannot process events you are in effect disabling all chrome shortcuts.
    – Barak Gall
    Jul 6, 2016 at 22:29
  • In Chrome, I open a new tab and double click the x button by the tab name. It seems to work, though I'm not sure why the first click isn't registered. May 21, 2019 at 0:49
12

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.

10

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.

0
4

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.

3
  • 5
    -1 redundant. OP: "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)!" I've experienced the same problem as well, with the dialog not always showing up.
    – mellamokb
    Apr 26, 2011 at 1:28
  • See stackoverflow.com/questions/905322/… for possible reason why "the dialog not always showing up".
    – user66001
    Aug 11, 2013 at 22:00
  • In modern browsers, this does not kill the offending script, it will kill the tab.
    – bryc
    Jul 18, 2022 at 13:49
4

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.

2

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

0

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.

0

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.

0

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.

0

Last resort: close the browser from the OS. I had good results closing Chrome from the taskbar in Windows 10 while an infinite loop ran. If that didn't work I would have killed the browser in Task Manager as above. On other OS's, of course the exact technique would vary. Infinite recursions are less a problem. All systems I've seen limit the size of the execution stack, and interrupt with a runtime error eventually once infinite recursion leads to stack overflow (no pun intended).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.