在 DOM 就绪事件上加载大量脚本片段的模式,而 IE 8 不会弹出脚本超时警告

发布于 2024-11-02 10:20:59 字数 1252 浏览 2 评论 0原文

我正在开发一个包含大量 JavaScript 的界面 - 所有小型的、独立的代码片段,旨在在界面上执行各种操作。

所有片段(或功能块,如果您愿意的话)都放置在同一范围内; DOM 就绪事件。这样各个片段就能够共享一些作用域全局变量。

它的结构如下...

$(function ()
{
    var SCOPED_GLOBAL_1;
    var SCOPED_GLOBAL_2;
    // Etc.


    // A snippet of code designed to enable X functionality

    // Another snippet of code designed to enable Y functionality

    // Etc.
});

使用这种方法,我会收到可怕的脚本超时弹出窗口,因为正在执行大量代码。

Internet Explorer 8 确定脚本超时的 基于拨打的电话数量...并且正在拨打很多电话...很多!


我已经做了一些测试,据我所知,避免弹出的唯一方法是使用 setTimeout,并逐渐执行代码块。

我尝试将代码拆分为对 DOM 就绪事件的多次调用,但 Internet Explorer 8 似乎将这些调用解释为一次调用。 (分析时的签名相同)。


如果我需要实现 setTimeout 方法,代码会变得混乱。共享作用域全局变量并不容易。

你们有模式可以帮助我吗?


更新

正如你们中的一些人所建议的,优化代码将是解决此问题的一种方法。我确信代码中有优化的空间,但据我所知;这里的问题只是大部分代码,而不是它的执行方式。

有许多预先设置的代码正在运行,这些代码初始化各种 UI 组件。正是这段代码在 Internet Explorer 8 中导致了问题...... 是由于调用的数量过多。而不是代码执行所需的时间。

该代码在所有其他浏览器中运行得又快又好,加载平均需要一秒半的时间(与正在发生的事情相比,这很快)。这里的问题是,Internet Explorer 8 根据执行的调用次数将脚本定义为缓慢,而所有其他浏览器(似乎还有 IE 9)根据执行代码所需的时间来定义超时。

I'm working on an interface with a lot of JavaScript - all small, self-contained snippets of code, designed to do various things on the interface.

All of the snippets (or blocks of functionality, if you will) are placed inside the same scope; the DOM ready event. This is so that the various snippets are able to share a few scoped global variables.

Here's how it's structured...

$(function ()
{
    var SCOPED_GLOBAL_1;
    var SCOPED_GLOBAL_2;
    // Etc.


    // A snippet of code designed to enable X functionality

    // Another snippet of code designed to enable Y functionality

    // Etc.
});

Using this approach, I get the dreaded script time-out pop-up, because of the large amount of code being executed.

Internet Explorer 8 determines script time-out's based on how many calls are made... And a lot of calls are being made... A lot!

I've done some testing, and as far as I can tell, the only way to avoid the pop-up is to use setTimeout, and gradually execute chunks of the code.

I've tried splitting up the code into multiple calls to the DOM ready event, but it seems that Internet Explorer 8 interprets those calls as if they were one call. (Same signature when profiling).

The code is gonna get messy if I need to implement the setTimeout approach. And sharing scoped globals won't be easy.

Have you guys got a pattern to help me out?

Update

As some of you have suggested, optimizing the code would be a way to handle this. I am sure that there is room for optimizations in the code, but as I see it; it is simply the bulk of code that's the issue here and not how it performs.

There are a lot of pre- set-up code being run, which initializes various UI components. It is this code that is causing problems in Internet Explorer 8... by the sheer number of calls being made. Not how long it takes for the code to execute.

The code runs fast and fine in all other browsers, taking on average a second and a half to load (which is fast, compared to how much stuff is going on). The problem here is that Internet Explorer 8 defines a script as being slow, by the number of calls being done, whilst all other browsers (and IE 9, seemingly) defines time-out's by the time it takes the code to execute.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻身的咸鱼 2024-11-09 10:20:59

如果没有重构,setTimeout 可能是本书中最简单的技巧。它也不必很混乱:

$(function () {
    var SCOPED_GLOBAL_1, SCOPED_GLOBAL_2;

    // Add code in blocks
    var codeBlocks = [];
    codeBlocks.push(function () {
        // A snippet of code
    });
    codeBlocks.push(function () {
        // Another snippet of code
    });

    // Execute code in blocks
    while (codeBlocks.length) {
        (function (func) {
            setTimeout(func, 0);
        })(codeBlocks.shift());
    }
});

这实际上创建了一个包含任意数量要执行的代码块的队列,并使用 setTimeout() 按顺序单独执行它们。

Short of refactoring, setTimeout is probably the simplest trick in the book. It doesn't have to be messy either:

$(function () {
    var SCOPED_GLOBAL_1, SCOPED_GLOBAL_2;

    // Add code in blocks
    var codeBlocks = [];
    codeBlocks.push(function () {
        // A snippet of code
    });
    codeBlocks.push(function () {
        // Another snippet of code
    });

    // Execute code in blocks
    while (codeBlocks.length) {
        (function (func) {
            setTimeout(func, 0);
        })(codeBlocks.shift());
    }
});

This essentially creates a queue of any number of blocks of code to execute, and executes them individually, in order, with setTimeout().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文