如何防止“停止运行此脚本”在浏览器? [英] How to prevent "Stop running this Script" in browsers?
问题描述
我有一个ASP.NET MVC页面,其中包含14列的JQuery Editable Datatable。
我有一个按钮(申请无结果)来做客户端计算并适用于该表中的所有行。
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.
当我们点击此按钮后,对每4行应用计算,它显示此停止运行脚本消息。
When we click on this button, after applying calculations for every 4 Rows, it is displaying this "Stop Running Script" Message.
我验证了设置。在Internet选项的高级选项卡中,禁用脚本调试(Internet Explorer)选项已选中。并且显示有关脚本错误的通知未选中。
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 Explorer 8.我现在不会在IE9上发生。但这是服务器,我们无法升级到IE9。
I am using Internet Explorer 8. I Now it does not happen on IE9. But this being the server, we cannot upgrade to IE9.
我做了研究并尝试了这两个选项,没有任何效果。
I did the Research and tried these two options and nothing worked.
示例(1):
http://www.codeproject.com/Tips/406739/Preventing-Stop-running-this-script-in-Browsers
任何人都有这个,任何建议都受到高度赞赏。
Anyone had this isse and any suggestions are highly appreciated.
这是抛出脚本消息的实际代码:
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();
通过参考示例(2)中的示例代码,我更改了代码,如下所示我仍然收到脚本消息:
//此函数是为了避免脚本运行消息
//This function is to avoid 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();
}
我做错了什么在这里?
推荐答案
问题是javascript是单线程的,所以如果有一个 需要的函数太长而无法完成 ,此功能可能导致UI无法响应。因此,浏览器将通过显示以下消息警告用户长时间运行的脚本:停止运行此脚本。这个问题的解决方案是:
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:
- 优化你的代码,这样功能就不会花这么长时间。
- 使用
setTimeout
将函数执行分解为很多足够短的部分。
- 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.
示例代码模式:
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);
您在示例(2)中采用的解决方案是正确的,但代码中存在问题。那是 setTimeout无法运行 。尝试更改你的代码:
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:
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, [])
}
};
};
修改函数ApplyNoFindings()
,试试这个:
if (++i > iTotalRecords)
{
UpdateSummaryLine();
UpdateSummaryLineReasonCode();
}
而不是:
if (++i < iTotalRecords)
{
ro.step();
}
else
{
UpdateSummaryLine();
UpdateSummaryLineReasonCode();
}
注意:未经测试,只是让你知道它应该如何工作
Note: not tested, just give you an idea how it should work
这篇关于如何防止“停止运行此脚本”在浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!