jQuery append in loop - DOM does not update until the end

后端 未结 4 451
感情败类
感情败类 2020-12-10 03:10

When looping over a large collection and appending it to the DOM, the DOM only refreshes after all items have been appended. Why doesn\'t the DOM update after each app

4条回答
  •  轮回少年
    2020-12-10 03:29

    Generally, please don't touch DOM too many times. It is a performance killer as you have already observed.

    var result="";
    for (i=0; i<5000; i++) {
        result+='
  • Line Item
  • '; } $('#collection').append(result);

    In this way, you touch DOM only once!

    An alternative way is using array.

    var result=[];
    for (i=0; i<5000; i++) {
        result.push('
  • Line Item
  • '); } $('#collection').append(result.join(""));

提交回复
热议问题