简体   繁体   English

如果我在代码中犯了一个错误并导致javascript中的无限循环并且它继续调用alert(),是否有办法停止循环?

[英]if i make a mistake in code and cause an infinite loop in javascript and it keeps on calling alert(), is there a way out to stop the loop?

sometimes i use debug code to alert something in javascript (for example, matching something in regular expression), but forget a modifier and and the alert is in an infinite loop (or if the loop matches the pattern 300 times). 有时我使用调试代码来警告javascript中的某些内容(例如,匹配正则表达式中的内容),但忘记修饰符并且警报处于无限循环中(或者如果循环匹配模式300次)。 If using Firefox, the alert keeps on coming out, and there is no way to even close the tab, the window, or the app. 如果使用Firefox,则警报会一直显示,甚至无法关闭选项卡,窗口或应用程序。

If I force exit, it will close all the tabs and even other windows of Firefox... is there actually a way to stop the loop more gracefully? 如果我强行退出,它将关闭所有标签甚至Firefox的其他窗口...实际上有一种方法可以更优雅地停止循环吗?

The short answer is: No. 最简洁的答案是不。

This is one good reason to use Firebug and the console.log function. 这是使用Firebug和console.log函数的一个很好的理由。 Which, ironically, will cause the "stop script because it's running away dialog" to not display in some cases, meaning you are right back where you are now. 具有讽刺意味的是,这将导致“停止脚本,因为它正在逃避对话”,在某些情况下不显示,这意味着你现在回到了现在的位置。

Chrome and Opera have this feature. Chrome和Opera都有此功能。 IE doesn't, Apple Safari doesn't either. IE没有,Apple Safari也没有。

Not a native solution but you could try this grease-monkey script: http://www.tumuski.com/2008/05/javascript-alert-cancel-button/ 不是原生的解决方案,但你可以试试这个油脂猴子脚本: http//www.tumuski.com/2008/05/javascript-alert-cancel-button/

Also, you could simply override the alert function to use a confirm dialog instead and stop showing alerts if the confirm is canceled: 此外,您可以简单地覆盖警报功能以使用确认对话框,并在取消确认时停止显示警报:

var displayAlerts = true;

And then: 然后:

function alert(msg) {
  if (displayAlerts) {
     if (!confirm(msg)) {
       displayAlerts = false;           
     }
  }
}

Looks like you can in firefox: 看起来你可以在Firefox中:

  1. Hold Ctrl 按住Ctrl键
  2. Hold Enter 按Enter键
  3. Press F4 once 按F4一次

According to the blog post, it doesn't work in all cases, more information here: puremango.co.uk 根据博客文章,它并不适用于所有情况,更多信息请访问: puremango.co.uk

谷歌浏览器允许您阻止显示其他警报。

I've written a Firefox extension to combat this problem. 我写了一个Firefox扩展来解决这个问题。

https://addons.mozilla.org/en-US/firefox/addon/13176 https://addons.mozilla.org/en-US/firefox/addon/13176

Install Greasemonkey and go to this page: http://www.tumuski.com/2008/05/javascript-alert-cancel-button/ 安装Greasemonkey并转到此页面: http//www.tumuski.com/2008/05/javascript-alert-cancel-button/

After you install this script for greasemonkey, any alert dialog window will have a cancel button which it will stop the javascript. 为greasemonkey安装此脚本后,任何警告对话框窗口都会有一个取消按钮,它将停止javascript。

[tested on IE 11] If you need to close the web browser and don't want to ctrl-alt-del for whatever reason (in my case my bad javascript infinite alert loop was running on citrix which would automatically reload my last session after a ctrl-alt-del from the cache memory which I can't clear) [在IE 11上测试]如果你需要关闭网页浏览器并且不想出于任何原因ctrl-alt-del(在我的情况下,我的错误的javascript无限警报循环在citrix上运行,这将自动重新加载我的上一个会话之后来自缓存的ctrl-alt-del,我无法清除)

you can bug your way out by holding down alt-F4 and spamming the X (to close the browser window). 你可以通过按住alt-F4并发送垃圾邮件 (关闭浏览器窗口)来解决问题。

This works really fast... The alt-F4 closes the alert boxes really fast and you will notice the X button at the top of your browser flickering. 这非常快...... alt-F4非常快地关闭警报框,你会注意到浏览器顶部的X按钮闪烁。 This gives you very small windows of time where you can close it between alerts. 这为您提供了非常小的时间窗口,您可以在警报之间关闭它。

Note: if you have multiple tabs open you need to close them from the icon on the control bar at the bottom of your screen first because the "close all tabs message" will interfere. 注意:如果您打开了多个选项卡,则需要先从屏幕底部控制栏上的图标将其关闭,因为“关闭所有选项卡消息”会产生干扰。

No way. 没门。 Good thing is though most browsers have infinite recursion protection, but that's offtopic. 好的是,虽然大多数浏览器都有无限的递归保护,但这是offtopic。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM