最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you interrupt JS in browser? - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question asked Mar 11, 2012 at 22:20 skratskrat 5,5623 gold badges34 silver badges48 bronze badges 2
  • 4 I have to admit that this was meant more as "motivation" for browser developers, to fix the situation. – skrat Commented Mar 13, 2012 at 16:24
  • 1 Frankly existing JS executors are stupid that they cant do something so simple. – Pacerier Commented Jun 18, 2017 at 22:25
Add a comment  | 

4 Answers 4

Reset to default 7

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.

There is no such thing as a Ctrl + C for JavaScript. The browsers that executes JavaScript are usually protective of themselves. If any JavaScript hangs, they'll throw a dialog asking if the user wants to stop the JavaScript.

The timeout duration can usually be found in the browser's settings. You can find how to do it for FireFox here: http://kb.mozillazine.org/Dom.max_script_run_time

From what little insight I have into your ways of working, how I would proceed is:

  • Only execute the script on an event like button click. This would prevent script running onload
  • Chrome allows you to set break points in your js code in the scripts tab of developer tools

There is no real "interrupter" for running Javascript code in a browser. ECMAscript gets executed in the so called "UI thread", which means that all rendering stuff happens in the same queue that ECMAscript code gets executed.

That in turn means, an infinite loop in ECMAscript automatically hangs the whole browsers interaction.

The only way to avoid that is to write clear, clean code. If it happens anyway, most browsers realize that the UI thread is busy for too long and asks the user if he wants to cancel the running javascript processes. If you don't want to wait for that, your only choice is to kill the whole browser / tab process.

However, if you are aware that some part of your script does possibly cause an infinite loop, you can either set breakpoints manually in some sort of developer tools or you can insert the debugger; keyword directly into your script. That causes the javascript interpreter to halt at the current line and you have a chance to analyse the next code (while conditions for instance) and cancel the execution if it looks bad.

发布评论

评论列表(0)

  1. 暂无评论