在 IE 中绕过 JavaScript 长时间运行警告对话框

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

Bypassing JavaScript long running warning dialog in IE

javascriptinternet-explorer

提问by Xi Zhang

I need to run a super long JavaScript on my page. The client is complaining that IE shows a warning dialog for the script being too long. Unfortunately, there is no way we can reduce the length of the script, so I am trying to find a bypass for the problem.

我需要在我的页面上运行一个超长的 JavaScript。客户端抱怨 IE 显示脚本太长的警告对话框。不幸的是,我们没有办法减少脚本的长度,所以我试图找到解决这个问题的方法。

According to Microsoft support website:

根据微软支持网站

IE tracks the total number of executed script statements and resets the value each time that a new script execution is started, such as from a timeout or from an event handler. It displays a "long-running script" dialog box when that value is over a threshold amount.

IE 跟踪已执行脚本语句的总数,并在每次启动新脚本执行时重置该值,例如超时或事件处理程序。当该值超过阈值时,它会显示一个“长时间运行的脚本”对话框。

However I have tried to use both setInterval() and setTimeout() to break my script into pieces, but none is working. The browser I am using is IE8. My code is as following:

但是,我尝试同时使用 setInterval() 和 setTimeout() 将我的脚本分成几部分,但都没有奏效。我使用的浏览器是IE8。我的代码如下:

<html>
<head>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.min.js"></script>
</head>
<body>
    <div id ="test"></div>
    <div id ="log"></div>
</body>
<script>
    var repeat =0;

    function heavyTask(){
        if (repeat<50){
            y = longRun();
            setTimeout("heavyTask()",100);
        }else{
            $('#test').html("done!");
        }
    }
    function longRun(){
        for(var i =0; i<20000;i++){ }
        repeat++;
        $('#log').append('<div>repeat: '+ repeat +'</div>');
    };

    $(document).ready(function () {
        setTimeout("heavyTask()",100);
    });
</script></html>

In order to make the code work, you have to edit Registry, go to HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Styles, and set the DWORD value called "MaxScriptStatements" to 100,000. If the Styles key is not present, create a new key that is called Styles.

为了使代码工作,您必须编辑注册表,转到 HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Styles,并将名为“MaxScriptStatements”的 DWORD 值设置为 100,000。如果 Styles 键不存在,则创建一个名为 Styles 的新键。

Thanks,

谢谢,

采纳答案by Diodeus - James MacFarlane

This processing limit is set by the browser, not JavaScript.

此处理限制由浏览器而非 JavaScript 设置。

You can break your process down into smaller steps.

您可以将流程分解为更小的步骤。

See this question: How can I give control back (briefly) to the browser during intensive JavaScript processing?

请参阅此问题:在密集的 JavaScript 处理期间,如何将控制权(简要地)交还给浏览器?

回答by Detect

just some syntax errors... http://jsfiddle.net/Detect/HnpCr/3/

只是一些语法错误... http://jsfiddle.net/Detect/HnpCr/3/