使用setTimeout绕过IE的长时间运行脚本警告

发布于 2024-12-07 19:50:45 字数 663 浏览 2 评论 0原文

我以前问过这个问题,并在网上找到了一些关于这个主题的文章,但我一生都无法弄清楚这一点。我有一组计算模型的 Javascript 函数,但是存在大量循环,导致脚本需要一段时间(约 4 秒)。我不介意处理时间,但 IE 会提示警告,因为执行次数太多。

我尝试过优化我的代码,但我根本无法减少执行次数以绕过 IE 警告。因此,我认为我必须使用setTimeout函数来重置IE中的计数器。

代码很长,我只是不知道如何在 IE 中正确实现 setTimeout。我尝试模仿发现的代码 此处,但由于某种原因,它最终在第 100 次迭代后崩溃。

我不知道这是否常见,但是如果我将代码发送给他们,有人介意看一下吗?我只是不想把它贴在这里,因为它太长了。

谢谢!

编辑:按照一些人的建议,我已将代码放在 JSfiddle 上。您可以在此处找到它。感谢您的建议,如有任何问题请告诉我!

I've asked about this before, and have found a few articles online regarding this subject, but for the life of me I cannot figure this out. I have a set of Javascript functions that calculate a model, but there's a ton of looping going on that makes the script take a while (~4 seconds). I don't mind the processing time, but IE prompts with a warning since there are so many executions.

I've tried optimizing my code, but I simply can't cut down the number of executions enough to bypass the IE warning. Therefore, I figure that I must use the setTimeout function to reset the counter in IE.

The code is quite long, and I just can't figure out how to properly implement setTimeout in IE. I've tried mimicking the code found here, but for some reason it ends up crashing after the 100th iteration.

I don't know if this is common at all, but would anyone mind taking a look at the code if I sent it to them? I just wouldn't want to post it on here because it's quite lengthy.

Thanks!

EDIT: I've placed my code on JSfiddle as some people have suggested. You can find it here. Thanks for the suggestion and let me know if there are any questions!

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

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

发布评论

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

评论(1

瞎闹 2024-12-14 19:50:45

我使用的基本方法是将工作分成批次。每个批次都在一个“时期”内完成,并且在批次完成时,它调用 setTimeout() 来开始下一个时期。

假设您要运行 1000 次迭代;您可以将其分成 100 个批次。

function doTheWork(operation, cycles, callback) {
    var self = this, // in case you need it
        cyclesComplete = 0,
        batchSize = 100;

    var doOneBatch = function() {
        var c = 0;
        while(cyclesComplete < cycles) {
            operation();
            c++;
            if(c >= batchSize) {
                // may need to store interim results here
                break;
            }
            cyclesComplete++;
        }
        if (cyclesComplete < cycles) {
            setTimeout(doOneBatch, 1);
        }
        else {
            callback(); // maybe pass results here
        }
    };

    // kickoff
    doOneBatch();
    return null;
};

The basic approach I use is to segment the work into batches. Each batch is done in one "epoch" and at the completion of the batch, it calls setTimeout() to kick off the next epoch.

Suppose you have 1000 iterations to run; you can segment it into batches of 100.

function doTheWork(operation, cycles, callback) {
    var self = this, // in case you need it
        cyclesComplete = 0,
        batchSize = 100;

    var doOneBatch = function() {
        var c = 0;
        while(cyclesComplete < cycles) {
            operation();
            c++;
            if(c >= batchSize) {
                // may need to store interim results here
                break;
            }
            cyclesComplete++;
        }
        if (cyclesComplete < cycles) {
            setTimeout(doOneBatch, 1);
        }
        else {
            callback(); // maybe pass results here
        }
    };

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