jQuery 如何防止浏览器中的“停止运行此脚本”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18403448/
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
How to prevent "Stop running this Script" in browsers?
提问by Rita
I have an ASP.NET MVC page that has JQuery Editable Datatable that has 14 columns. I have a button (Apply No Findings)to do Client side calcultions snd apply for all Rows in that table.
我有一个 ASP.NET MVC 页面,其中包含 14 列的 JQuery 可编辑数据表。我有一个按钮(不应用结果)来执行客户端计算并应用于该表中的所有行。
When we click on this button, after applying calculations for every 4 Rows, it is displaying this "Stop Running Script" Message.
当我们单击此按钮时,在每 4 行应用计算后,它会显示此“停止运行脚本”消息。
I verified the settings. In the Internet Options, Advanced tab, "Disable Script debugging(Internet Explorer)" option is Checked. And "Display a Notification about Script Error" is Unchecked.
我验证了设置。在 Internet 选项的高级选项卡中,选中“禁用脚本调试(Internet Explorer)”选项。并且“显示有关脚本错误的通知”未选中。
I am using Internet Explorer 8. I Now it does not happen on IE9. But this being the server, we cannot upgrade to IE9.
我正在使用 Internet Explorer 8。我现在它不会发生在 IE9 上。但这是服务器,我们无法升级到 IE9。
I did the Research and tried these two options and nothing worked.
我做了研究并尝试了这两个选项,但没有任何效果。
Example(1):http://www.codeproject.com/Tips/406739/Preventing-Stop-running-this-script-in-Browsers
示例(1):http : //www.codeproject.com/Tips/406739/Preventing-Stop-running-this-script-in-Browsers
示例(2):http : //www.picnet.com.au/blogs/guido/post/2010/03/04/how-to-prevent-stop-running-this-script-message-in-browsers/
Anyone had this isse and any suggestions are highly appreciated.
任何人都有这个问题,任何建议都受到高度赞赏。
This is actual code that is throwing the Script Message:
这是抛出脚本消息的实际代码:
for(i < iTotalRecords;i++)
{
var row = oTableAuditLines.fnGetData(i);
oTableAuditLines.fnUpdate("NF",i,1);
UndoHCPCS(row,i);
UndoHCPCSModCodes(row,i);
UndoLineUnitCount(row,i);
oTableAuditLines.fnUpdate("", i, 6); //Reset Denial Reason Code
UndoNonCoveredCharges(row,i);
CalculateAmountPaidAtLine(row,i);
CalculateEstimatedRecoveryAmountAtLine(row,i);
}
UpdateSummaryLine();
UpdateSummaryLineReasonCode();
By referring sample code in Example(2), I changed the code as below and I am still getting the Script message:
通过引用示例(2)中的示例代码,我更改了代码如下,但仍然收到脚本消息:
//This function is to avoid Script Running Message
//这个功能是为了避免Script Running Message
RepeatingOperation = function(op, yieldEveryIteration)
{
var count = 0;
var instance = this;
this.step = function(args)
{
if (++count >= yieldEveryIteration)
{
count = 0;
setTimeout(function() { op(args); }, 1, [])
return;
}
op(args);
};
};
function ApplyNoFindings()
{
var i = 0;
var ro = new RepeatingOperation(function()
{
var row = oTableAuditLines.fnGetData(i);
oTableAuditLines.fnUpdate("NF",i,1);
UndoHCPCS(row,i);
UndoHCPCSModCodes(row,i);
UndoLineUnitCount(row,i);
oTableAuditLines.fnUpdate("", i, 6); //Reset Denial Reason Code
UndoNonCoveredCharges(row,i);
CalculateAmountPaidAtLine(row,i);
CalculateEstimatedRecoveryAmountAtLine(row,i);
if (++i < iTotalRecords)
{
ro.step();
}
else
{
UpdateSummaryLine();
UpdateSummaryLineReasonCode();
}
}, 100);
ro.step();
}
}
What am i doing wrong here?
我在这里做错了什么?
回答by Khanh TO
The problem is javascript is single-threaded, so if there is a function that takes too long to complete, this function could cause the UI not responding. Therefore, the browser will warn the user about long running script by displaying the message: "Stop running this script". The solutions to this problem are:
问题是 javascript 是单线程的,所以如果有一个函数需要很长时间才能完成,这个函数可能会导致 UI 没有响应。因此,浏览器将通过显示消息来警告用户有关长时间运行的脚本:“停止运行此脚本”。这个问题的解决方案是:
- Optimize your code so the function does not take so long.
- Use
setTimeout
to break the function execution into many pieces that are short enough.
- 优化您的代码,使该功能不会花费很长时间。
- 用于
setTimeout
将函数执行分成足够短的许多部分。
Example code pattern:
示例代码模式:
var array = []; //assume that this is a very big array
var divideInto = 4;
var chunkSize = rowCount/divideInto;
var iteration = 0;
setTimeout(function doStuff(){
var base = chunkSize * iteration;
var To = Math.min(base+chunkSize,array.length);
while (base < To ){
//process array[base]
base++;
}
iteration++;
if (iteration < divideInto)
setTimeout(doStuff,0); //schedule for next phase
},0);
The solution you take in your Example(2) is correct, but there is a problem in your code. That's the setTimeout does not run. Try changing your code like this:
您在 Example(2) 中采用的解决方案是正确的,但是您的代码存在问题。那是setTimeout 没有运行。尝试像这样更改您的代码:
RepeatingOperation = function(op, yieldEveryIteration)
{
var count = 0;
var instance = this;
this.step = function(args)
{
op(args);
if (++count <= yieldEveryIteration)
{
setTimeout(function() { instance.step(args); }, 1, [])
}
};
};
Modify your function ApplyNoFindings()
, try this:
修改你的function ApplyNoFindings()
,试试这个:
if (++i > iTotalRecords)
{
UpdateSummaryLine();
UpdateSummaryLineReasonCode();
}
instead of:
代替:
if (++i < iTotalRecords)
{
ro.step();
}
else
{
UpdateSummaryLine();
UpdateSummaryLineReasonCode();
}
Note: not tested, just give you an idea how it should work
注意:未经测试,只是让您知道它应该如何工作
回答by nahab
Despite another answer already accepted I want to place here general information for developers who will have this problem in feature:
尽管已经接受了另一个答案,但我想在此处为在功能中遇到此问题的开发人员提供一般信息:
By this link you can find information how to fix this from user side http://support.microsoft.com/kb/175500- user can add one Registry key.
通过此链接,您可以找到如何从用户端解决此问题的信息http://support.microsoft.com/kb/175500- 用户可以添加一个注册表项。
Also you can find there information about when this "Running slow" message appears:
您还可以找到有关何时出现此“运行缓慢”消息的信息:
By default, the key does not exist. If the key has not been added, the default threshold limit for the time-out dialog box is 5,000,000 statementsfor Internet Explorer 4 and later versions.
默认情况下,密钥不存在。如果尚未添加密钥,则Internet Explorer 4 和更高版本的超时对话框的默认阈值限制为5,000,000 条语句。
As you see slowness measured in javascript statements, not in time.
正如您所看到的,速度是在 javascript 语句中衡量的,而不是时间。
回答by Steven Lizarazo
You can also consider HTML 5 workers
你也可以考虑 HTML 5 工人
A web worker is a JavaScript running in the background, without affecting the performance of the page. When executing scripts in an HTML page, the page becomes unresponsive until the script is finished. A web worker is a JavaScript that runs in the background, independently of other scripts, without affecting the performance of the page.
Web Worker 是在后台运行的 JavaScript,不会影响页面的性能。在 HTML 页面中执行脚本时,页面会在脚本完成之前无响应。Web Worker 是在后台运行的 JavaScript,独立于其他脚本,不会影响页面的性能。
Creating a new worker is simple. All you need to do is call the Worker() constructor, specifying the URI of a script to execute in the worker thread, set the worker's Worker.onmessage property to an appropriate event handler function.
创建一个新的 worker 很简单。您需要做的就是调用 Worker() 构造函数,指定要在工作线程中执行的脚本的 URI,将工作线程的 Worker.onmessage 属性设置为适当的事件处理函数。
var myWorker = new Worker("my_task.js");
myWorker.onmessage = function (oEvent) {
console.log("Called back by the worker!\n");
};
Alternatively, you could use addEventListener() :
或者,您可以使用 addEventListener() :
var myWorker = new Worker("my_task.js");
myWorker.addEventListener("message", function (oEvent) {
console.log("Called back by the worker!\n");
}, false);
myWorker.postMessage(""); // start the worker.
You can see more details at: https://developer.mozilla.org/en-US/docs/Web/Guide/Performance/Using_web_workers
您可以在以下位置查看更多详细信息:https: //developer.mozilla.org/en-US/docs/Web/Guide/Performance/Using_web_workers