在 Internet Explorer 中禁用长时间运行的脚本消息 [英] Disabling the long-running-script message in Internet Explorer

查看:27
本文介绍了在 Internet Explorer 中禁用长时间运行的脚本消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JavaScript 函数,其中包含一个迭代了很多次的 for 循环.
调用该函数后,IE浏览器显示如下信息:

I have a JavaScript function that contains a for loop that iterates so many times.
After calling this function, the IE browser displays this message:

停止运行这个脚本?
此页面上的脚本导致您的 Web 浏览器运行缓慢.如果它继续运行,您的计算机可能会变得无响应.

Stop running this script?
A script on this page is causing your web browser to run slowly. If it continues to run, your computer might become unresponsive.

我该如何解决这个问题?
无论如何,我可以从 IE 中禁用此消息吗?

How can I fix this?
is there anyway I can disable this message from IE?

推荐答案

当 Internet Explorer 达到一段 JavaScript 的最大同步指令数时,将显示此消息.默认最大值为 5,000,000 条指令,您可以通过编辑注册表在单台机器上增加此数量.

This message displays when Internet Explorer reaches the maximum number of synchronous instructions for a piece of JavaScript. The default maximum is 5,000,000 instructions, you can increase this number on a single machine by editing the registry.

Internet Explorer 现在会跟踪已执行脚本语句的总数,并在每次启动新脚本执行时重置该值,例如从超时或事件处理程序开始,针对具有脚本引擎的当前页面.当该值超过阈值时,Internet Explorer 会显示一个长时间运行的脚本"对话框.

为可能正在查看您的页面的所有用户解决问题的唯一方法是使用计时器分解循环执行的迭代次数,或者重构您的代码,使其不需要处理尽可能多的指令.

The only way to solve the problem for all users that might be viewing your page is to break up the number of iterations your loop performs using timers, or refactor your code so that it doesn't need to process as many instructions.

用定时器打破循环相对简单:

Breaking up a loop with timers is relatively straightforward:

var i=0;
(function () {
    for (; i < 6000000; i++) {
        /*
            Normal processing here
        */

        // Every 100,000 iterations, take a break
        if ( i > 0 && i % 100000 == 0) {
            // Manually increment `i` because we break
            i++;
            // Set a timer for the next iteration 
            window.setTimeout(arguments.callee);
            break;
        }
    }
})();

这篇关于在 Internet Explorer 中禁用长时间运行的脚本消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆