首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在后台构建dom-subtree

在后台构建dom-subtree
EN

Stack Overflow用户
提问于 2018-06-15 07:16:02
回答 1查看 52关注 0票数 1

在javascript中构建一个大而复杂的dom-subtree是相当缓慢和昂贵的。有没有可能在后台构建这样一棵树,也许使用worker,然后在构建完成后才将其附加到主dom树上?

我知道工作人员无法访问dom和xml库,但也许有一种常见的解决方法可以在需要时延迟加载子树。

代码语言:javascript
复制
var c = document.createDocumentFragment();

// takes several seconds to execute, should not block main thread.
// may be inside a worker...
buildLargeAndComplexSubtree(c);

// append subtree to the main tree in the main thread.
document.body.appendChild(c);
EN

回答 1

Stack Overflow用户

发布于 2018-06-15 07:57:44

也许使用setTimeout将有助于将其从主线程中移除并提高性能:

代码语言:javascript
复制
setTimeout(function () {
    var c = document.createDocumentFragment();
    // takes several seconds to execute, should not block main thread.
    // may be inside a worker...
    buildLargeAndComplexSubtree(c);
    // append subtree to the main tree in the main thread.
    document.body.appendChild(c);
}, 0);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50867166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档