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

后端 未结 4 462
感情败类
感情败类 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:33

    you need to give back control to the browser every once in a while:

        var current = 0
    
    
        function draw() {
            var next = current + 10
            for(var i = current; i < next; i++) {
                $('#collection').append('
  • Line Item
  • '); } current = next setTimeout(draw, 50); } draw();

提交回复
热议问题