javascript预加载复杂数据

发布于 2024-12-29 08:02:17 字数 344 浏览 0 评论 0原文

我有一个简单的网页,用于介绍一组更复杂的数据。

这组复杂的数据需要花费大量时间来渲染。

对于新用户,当他们到达简单的网页时,我开始在内存中渲染复杂的页面,创建 html 元素等,而不将它们插入到 dom 中。

然而,这仍然明显很慢,并导致介绍页面上的滚动停止和滞后。

我正在考虑使用网络工作者。但在我做出类似的事情之前,我会询问这是否真的是可行的方法,或者是否还有其他可能的解决方案。谢谢。

编辑:

想想看,在渲染数据时我实际上并没有访问 dom,模板引擎将字符串放在一起来创建 html,所以 webworkers 完全没问题。

有没有人可以分享一下这个方法?

I have a simple webpage that serves as an introduction to a more complex set of data.

This complex set of data takes a lot of time to render.

For new users when they arrive at the simple webpage I start rendering the complex page in the memory, creating html elements, etc without inserting them into the dom.

However this is still visibly slow, and causes scrolling on the introduction page to stall and lag.

I'm thinking about using webworkers. But before I commit to something like that I'm inquiring if that's actually the way to go or if there are other solutions also possible. Thanks.

Edit:

Thinking about it I don't actually access the dom when rendering data, the templating engine puts strings together to create html, so webworkers are totally fine.

Is there anything anyone can share about this method?

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

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

发布评论

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

评论(2

演多会厌 2025-01-05 08:02:17

当然,网络工作者将是一条出路。但对于不支持 Web Worker 的浏览器(IE < 10),这里有一个后备方法:

如果您的代码是同步的,它将在运行时冻结浏览器。但是,如果您可以将处理分成适当大小的块,则可以将其分解得足以让浏览器有机会响应用户交互。

因此,这段代码:

for (var i = 0; i < data.length; i++) {
    processData(data[i]);
}

变成这样: 这

var i = 0;
(function processNext() {
    if (i < data.length) {
        processData(data[i++]);
        setTimout(processNext, 0);
    }
})();

并不理想 - 如果 processData() 执行太多操作,UI 仍然会出现令人无法接受的无响应情况。另一方面,如果它做的事情不多,它可能会大大减慢您的数据处理速度。在后一种情况下,您可以在调用 setTimeout 之前一次处理一定数量的迭代:

var i = 0;
(function processNext() {
    if (i < data.length) {
        for (var stop = i + 100; i < stop && i < data.length; i++) {
            processData(data[i]);
        }
        setTimout(processNext, 0);
    }
})();

Certainly web workers would be the way to go. But for browsers that don't support web workers (IE < 10), here is a fallback approach:

If your code is synchronous, it will freeze the browser while it is running. However, if you can chunk your processing into decent sized chunks, you can break it up just enough to allow the browser a chance to respond to user interactions.

So, this code:

for (var i = 0; i < data.length; i++) {
    processData(data[i]);
}

Becomes this:

var i = 0;
(function processNext() {
    if (i < data.length) {
        processData(data[i++]);
        setTimout(processNext, 0);
    }
})();

It's not ideal - if processData() does too much, the UI will still be unacceptably unresponsive. On the other hand, if it doesn't do very much, it can slow your data processing down a lot. In the latter case, you can process a certain number of iterations at a time before calling setTimeout:

var i = 0;
(function processNext() {
    if (i < data.length) {
        for (var stop = i + 100; i < stop && i < data.length; i++) {
            processData(data[i]);
        }
        setTimout(processNext, 0);
    }
})();
や莫失莫忘 2025-01-05 08:02:17

我认为 Web 工作者不适合您,因为他们无权访问 DOM。您将无法与原始网页进行交互,只能向其发送文本“消息”。

如果您只是进行计算并希望将其发送到原始网页,那么网络工作者会很棒。

var pi = Computepi();
self.postMessage('Pi is: ' + pi);

但是,您不能执行类似“

var canvas = document.createElement("canvas");
DrawComplexFractal(canvas);
self.postMessage(canvas);

没有可供网络工作人员使用的窗口或文档对象”之类的操作。

I do not think Web workers are right for you, as they do not have access to the DOM. You will not be able to interact with the original web page other then to send text "messages" over to it.

If you are just doing computations and want to send those over to the original webpage then web workers will be great.

var pi = Computepi();
self.postMessage('Pi is: ' + pi);

However you can not do something like

var canvas = document.createElement("canvas");
DrawComplexFractal(canvas);
self.postMessage(canvas);

There is no window or document object available to the web-worker.

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