Javascript:无响应的脚本错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2686118/
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-23 01:27:45  来源:igfitidea点击:

Javascript: Unresponsive script error

javascript

提问by aneuryzm

I get an error message from Firefox "Unresponsive script". This error is due to some javascript I added to my page.

我收到来自 Firefox“无响应脚本”的错误消息。这个错误是由于我添加到我的页面的一些 javascript。

I was wondering if the unresponsiveness are caused exclusively by code loops (function calling each other cyclically or endless "for loops") or there might be other causes ?

我想知道无响应是否完全是由代码循环引起的(函数循环地相互调用或无休止的“for 循环”)还是可能有其他原因?

Could you help me to debug these kind of errors ?

你能帮我调试这些错误吗?

thanks

谢谢

采纳答案by Damien Wilson

According to the Mozzila Knoledgebase:

根据Mozzila 知识库

When JavaScript code runs for longer than a predefined amount of time, Firefox will display a dialog that says Warning: Unresponsive Script. This time is given by the settings dom.max_script_run_time and dom.max_chrome_script_run_time. Increasing the values of those settings will cause the warning to appear less often, but will defeat the purpose: to inform you of a problem with an extension or web site so you can stop the runaway script.

当 JavaScript 代码运行的时间超过预定义的时间时,Firefox 将显示一个对话框,显示警告:无响应脚本。这个时间由设置 dom.max_script_run_time 和 dom.max_chrome_script_run_time 给出。增加这些设置的值将导致警告出现的频率降低,但会破坏目的:通知您扩展程序或网站的问题,以便您可以停止失控的脚本。

Furthermore:

此外:

Finding the source of the problem

To determine what script is running too long, click the Stop Script button on the warning dialog, then go to Tools | Error Console. The most recent errors in the Error Console should identify the script causing the problem.

找到问题的根源

要确定哪个脚本运行时间过长,请单击警告对话框上的停止脚本按钮,然后转到工具 | 错误控制台。错误控制台中的最新错误应确定导致问题的脚本。

Checking the error console should make it pretty obvious what part of your javascript is causing the issue. From there, either remove the offending piece of code or change it in such a way that it won't be as resource intensive.

检查错误控制台应该可以很明显地看出您的 javascript 的哪一部分导致了问题。从那里,要么删除有问题的代码段,要么以不占用资源的方式更改它。

EDIT:As mentioned in the comments to the author of the topic, Firebugis highly recommended for debugging problems with javascript. Jonathan Snookhas a useful video on using breakpoints to debug complex pieces of javascript.

编辑:正如对该主题作者的评论中所述,强烈建议使用Firebug来调试 javascript 问题。Jonathan Snook有一个关于使用断点调试复杂 javascript 的有用视频。

回答by Daniel Fernandez

One way to avoid this is to wrap your poor performant piece of code with a timeout like this:

避免这种情况的一种方法是用这样的超时来包装性能不佳的代码:

setTimeout(function() {
 // <YOUR TIME CONSUMING OPERATION GOES HERE>
}, 0);

This is not a bullet proof solution, but it can solve the issue in some cases.

这不是防弹解决方案,但在某些情况下可以解决问题。

回答by SVK

We need to follow these steps to stop script in Firefox.

我们需要按照以下步骤在 Firefox 中停止脚本。

Step 1 Launch Mozilla Firefox.

步骤 1 启动 Mozilla Firefox。

Step 2 Click anywhere in the address bar at the top of the Firefox window, to highlight the entire field.

步骤 2 单击 Firefox 窗口顶部地址栏中的任意位置,以突出显示整个字段。

Step 3 Type "about:config" (omit the quotes here and throughout) and press "Enter." A "This might void your warranty!" warning message may come up; if it does, click the "I'll be careful, I promise!" button to open the page that contains all Firefox settings.

步骤 3 输入“about:config”(省略此处和整个部分的引号),然后按“Enter”。A “这可能会使您的保修失效!” 可能会出现警告信息;如果是,请单击“我会小心,我保证!” 按钮打开包含所有 Firefox 设置的页面。

Step 4 Click once in the "Search" box at the top of the page and type "dom.max_script_run_time". The setting is located and displayed; it has a default value of 10.

步骤4 在页面顶部的“搜索”框中单击一次并键入“dom.max_script_run_time”。定位并显示设置;它的默认值为 10。

Step 5 Double-click the setting to open the Enter Integer Value window.

步骤 5 双击设置打开输入整数值窗口。

Step 6 Type "0" and click "OK" to set the value to zero. Firefox scripts now run indefinitely, and will not throw any script errors. Step 7

步骤 6 键入“0”并单击“确定”将值设置为零。Firefox 脚本现在无限期运行,并且不会抛出任何脚本错误。第 7 步

Restart Mozilla Firefox.

重新启动 Mozilla Firefox。

回答by Andrea

Excellent solution in this question: How can I give control back (briefly) to the browser during intensive JavaScript processing?, by using the Async jQuery Plugin. I had a similar problem and solved it by changing my $.each for $.eachAsync

这个问题的优秀解决方案:如何在密集的 JavaScript 处理期间将控制权(简要地)交还给浏览器?, 通过使用异步 jQuery 插件。我有一个类似的问题,并通过将我的 $.each 更改为 $.eachAsync 来解决它

回答by Jianhong

  1. there could be an infinite loop somewhere in the code
    • start by commenting out codes to identify which section is causing it
    • too many loops: there might be a chance that your counter variable name clashes, causing the variable to keep resetting, causing the infinite loop.
  2. try as much as possible to create hashes for your objects so much so that read time is O(1) and in a way caching those data
  3. avoid using js libsas some of the methods might cause overheads. eg. .htm()vs .innerHTML
  4. setTimeout() yes and no -- depends on how you chunkify your codes
  1. 代码中某处可能存在无限循环
    • 首先注释掉代码以确定哪个部分导致它
    • 循环太多:您的计数器变量名称可能会发生冲突,导致变量不断重置,从而导致无限循环。
  2. 尽可能多地为您的对象创建散列,以便读取时间为 O(1) 并以某种方式缓存这些数据
  3. 避免使用,js libs因为某些方法可能会导致开销。例如。.htm()对比.innerHTML
  4. setTimeout() 是和否——取决于你如何分块你的代码