简体   繁体   English

使用JavaScript阻止DOM解析

[英]Blocking DOM Parsing with JavaScript

I have a small problem. 我有一个小问题。 I have a table on a HTML page, which is filled by an asynchronous call in JS. 我在HTML页面上有一个表格,该表格由JS中的异步调用填充。 There are thousands of elements, and obviously they are filled with classes and all to make them pretty :) . 有成千上万个元素,显然它们充满了类,所有这些都使它们变得漂亮:)。

This is done like so: 这样做是这样的:

for(var i  = 0; i < elements.length; i++){
    var td = document.createElement("td");
    td.className = "all the pretty css";

    var button = document.createElement("a");
    button.className = "btn so pretty wow";
    td.appendChild(button);

    tableTr.appendChild(td);
}

However, this takes a lot of time, and by using console.time and console.timeEnd I was able to determine that the whole process takes about 100ms of JS execution, which means that the time is actually being queued by DOM parsing. 但是,这花费了大量时间,并且通过使用console.timeconsole.timeEnd我能够确定整个过程花费了大约100ms的JS执行时间,这意味着该时间实际上是由DOM解析排队的。 (Am I right?) (我对吗?)

So, I was wondering if there is any way to do something like: 所以,我想知道是否有任何办法可以做这样的事情:

Dom.stopParsing();

mySuperFunction();
anotherFunction();
thisTimeAsync(
    function(){
        Dom.parse();
);

And so, effectively reduce parsing time! 这样,有效地减少了解析时间!

Use document fragments : 使用文档片段

var frag = document.createDocumentFragment();

for(var i  = 0; i < elements.length; i++){
    var td = document.createElement("td");
    td.className = "all the pretty css";

    var button = document.createElement("a");
    button.className = "btn so pretty wow";
    td.appendChild(button);

    frag.appendChild(td);
}

tableTr.appendChild(frag);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM