Javascript 取消jsfiddle中的无限循环执行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7616143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:55:40  来源:igfitidea点击:

Cancel infinite loop execution in jsfiddle

javascriptgoogle-chromeinfinite-loopjsfiddle

提问by mellamokb

When you get an infinite loop in jsfiddle in Chrome, your only choice (that I know of) is to close the tab. Of course, this means you lose all your work in the current window! Is there an easy way to stop an infinitely executing script?

当您在 Chrome 中的 jsfiddle 中遇到无限循环时,您唯一的选择(我知道)是关闭选项卡。当然,这意味着您将丢失当前窗口中的所有工作!有没有一种简单的方法可以停止无限执行的脚本?

  1. I have the developer tools open because I was doing some debugging.
  2. I am able to pause the script and step through the loop.
  3. I can't find anywhere to stop the script.
  4. I can't modify the script or variables to stop the infinite loop (because the script execution occurs in an iframe on a separate domain, so modifying data in the iframe with JavaScript is not allowed and generates an Exception in the console).
  1. 我打开了开发人员工具,因为我正在做一些调试。
  2. 我能够暂停脚本并逐步完成循环。
  3. 我找不到任何地方可以停止脚本。
  4. 我无法修改脚本或变量以停止无限循环(因为脚本执行发生在单独域的 iframe 中,因此不允许使用 JavaScript 修改 iframe 中的数据并在控制台中生成异常)。

It all started because I decided to swap directions on my loop from

这一切都开始是因为我决定在我的循环中交换方向

for (var c = 0; c <= 11; c++)

to

for (var c = 12; c > 0; c++)

But as you can see above, I forgot to change it from c++to c--.

但是正如您在上面看到的,我忘记将其从 更改c++c--

Any ideas?? I still have the tab open and I'm hoping to get it back without closing the tab :-)

有任何想法吗??我仍然打开选项卡,我希望在不关闭选项卡的情况下将其取回:-)

采纳答案by mowwwalker

With the developer mode, go into resources and find your script and copy and paste it into a text document or a new window. If you can't find it in resources, do a search for a variable or line of code you used.

使用开发人员模式,进入资源并找到您的脚本并将其复制并粘贴到文本文档或新窗口中。如果在资源中找不到它,请搜索您使用的变量或代码行。

回答by Adam Ritter

How to do it without Developer Mode:

如何在没有开发者模式的情况下做到这一点:

  • Open a new tab
  • Open the Task Manager with Shift-Escape
  • Kill task
  • Use back button for the killed tab(JSFiddle won't run the script)
  • Fix bug
  • Update
  • 打开一个新标签
  • 使用 Shift-Escape 打开任务管理器
  • 杀死任务
  • 对被杀死的选项卡使用后退按钮(JSFiddle 不会运行脚本)
  • 修复错误
  • 更新

回答by qbolec

One way of breaking the infinite loop is to throw an unhandled exception, which will stop the execution of current call stack. To do so:

打破无限循环的一种方法是抛出一个未处理的异常,这将停止当前调用堆栈的执行。这样做:

  1. pause the debbuger
  2. find a promising statement inside the loop, for example a function call: foo.bar(args)
  3. in console, overwrite the function to throw : foo.bar=function(){throw 42;}
  4. unpause the debugger
  1. 暂停调试器
  2. 在循环中找到一个有希望的语句,例如函数调用: foo.bar(args)
  3. 在控制台中,覆盖要抛出的函数: foo.bar=function(){throw 42;}
  4. 取消暂停调试器

worked for me. I haven't tried, but I believe that by overloading getter or setter you can use the trick described above also for assignments and reads, not only for function calls. Also, by setting a variable to undefined, you may cause fatal error (and thus break the loop) if the field of this variable is used in the loop. For example delete foo.tabwill break foo.tab[42]or foo.tab.bar. For some reason, simply writting foo=undefinedin the console, will not do (perhaps it defines a variable local to the console window named foo).

为我工作。我还没有尝试过,但我相信通过重载 getter 或 setter,您还可以将上述技巧用于赋值和读取,而不仅仅是用于函数调用。此外,通过将变量设置为 undefined,如果在循环中使用此变量的字段,可能会导致致命错误(从而中断循环)。例如delete foo.tabwill breakfoo.tab[42]foo.tab.bar. 出于某种原因,简单地写foo=undefined在控制台中是行不通的(也许它定义了一个名为 foo 的控制台窗口的本地变量)。

回答by Lachmanski

I couldn't start Chrome's Task Manager while the looping tab was active. I had to select another tab. Then I pressed Shift-Escape to launch the Task Manager, kill the JSFiddle process, re-select the tab, and hit the back button to display the page without running the script.

当循环选项卡处于活动状态时,我无法启动 Chrome 的任务管理器。我不得不选择另一个标签。然后我按下 Shift-Escape 启动任务管理器,杀死 JSFiddle 进程,重新选择选项卡,然后点击后退按钮显示页面而不运行脚本。

JSFiddle loads the "result" panel using an iframe. As of June 2016, the URL for the frame is the Fiddle URL plus "/show/". I inspected the source code of the iframe and discovered that it loads yet another URL with "/light/". One of these URLs should contain your source code.

JSFiddle 使用 iframe 加载“结果”面板。截至 2016 年 6 月,框架的 URL 是 Fiddle URL 加上“/show/”。我检查了 iframe 的源代码,发现它加载了另一个带有“/light/”的 URL。这些 URL 之一应包含您的源代码。

If your browser supports the view-source URI scheme, you may access your fiddle's source code as follows:

如果您的浏览器支持view-source URI scheme,您可以按如下方式访问您的小提琴的源代码:

  • view-source:jsfiddle.net/user_name/fiddle_hash/revision/light/
  • view-source:jsfiddle.net/user_name/fiddle_hash/revision/show/
  • 查看源:jsfiddle.net/user_name/fiddle_hash/revision/light/
  • 查看源:jsfiddle.net/user_name/fiddle_hash/revision/show/

回答by David

Run Process Explorer and kill the chrome process that's using lots of CPU...it will "crash" the page and let you reload...

运行进程资源管理器并杀死使用大量 CPU 的 chrome 进程......它会“崩溃”页面并让你重新加载......

回答by Brad Lensing

I had file stuck in a loop and would freeze up the dashboard on JSFiddle as well.

我的文件卡在一个循环中,并且也会冻结 JSFiddle 上的仪表板。

  • The only way I could clear was to disable JavaScript in the preferences tab while I had a New Fiddle page open in a different Chrome tab.
  • Then navigate to All Fiddles.
  • Delete the Fiddle and then it would give a 404 error.
  • Turn on the JavaScript and reload the Dashboard.
  • Was able to continue on making and editing fiddles after that.
  • 我可以清除的唯一方法是在不同的 Chrome 选项卡中打开一个 New Fiddle 页面时,在首选项选项卡中禁用 JavaScript。
  • 然后导航到所有小提琴。
  • 删除小提琴,然后它会给出 404 错误。
  • 打开 JavaScript 并重新加载仪表板。
  • 之后能够继续制作和编辑小提琴。

回答by kwinkunks

In case others are stuck after reading the other answers, here's what worked for me (Chrome, Mac). For me the JSFiddle tab was 'stuck' but the rest of Chrome responsive. I had the JavaScript Console open on the Resources pane, but it was unresponsive too. Reloading the page in this state didn't help because JSFiddle would give me the script before I got anything at all.

如果其他人在阅读其他答案后被卡住,这对我有用(Chrome,Mac)。对我来说,JSFiddle 选项卡被“卡住”,但 Chrome 的其余部分响应。我在“资源”窗格上打开了 JavaScript 控制台,但它也没有响应。在这种状态下重新加载页面没有帮助,因为 JSFiddle 会在我得到任何东西之前给我脚本。

In the end this worked for me; maybe it will help you too...

最后这对我有用;也许它也会帮助你...

  • While the page is unresponsive, go to Chrome > Preferences > Privacyand disable JavaScript.
  • Wait for page to die (about 4 or 5 minutes for me);?the sad face icon comes up in that tab.
  • Hit the back button. It should look like JSFiddle is loading, but it won't because funnily enough JSFiddle needs JavaScript just to render a page.
  • View > Developer > View source
  • My script, only slightly mangled, was sitting there all innocent like in a divcalled 'panel_js'.
  • Highlight, copy, breathe again, learn lesson.
  • 当页面没有响应时,转到Chrome > Preferences > Privacy并禁用 JavaScript。
  • 等待页面消失(对我来说大约需要 4 或 5 分钟);?悲伤的脸图标出现在该选项卡中。
  • 点击后退按钮。它应该看起来像 JSFiddle 正在加载,但它不会,因为有趣的是 JSFiddle 只需要 JavaScript 来呈现页面。
  • View > Developer > View source
  • 我的剧本,只是略有损坏,像在div被调用的'panel_js'.
  • 突出显示,复制,再次呼吸,吸取教训。

回答by Rajavanya Subramaniyan

Same problem came up here, I opened up the javascript console in a script paused state. (Paused it using the Developer Tools)

同样的问题出现在这里,我在脚本暂停状态下打开了 javascript 控制台。(使用开发者工具暂停它)

Then I changed the variable value so that the while loop would end.

然后我更改了变量值,以便 while 循环结束。