1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Document</title>
    8. </head>
    9. <body>
    10. <ul>控件</ul>
    11. <script>
    12. setTimeout(() => {
    13. // 插入十万条数据
    14. const total = 100000
    15. // 一次插入 20 条,如果觉得性能不好就减少
    16. const once = 20
    17. // 渲染数据总共需要几次
    18. const loopCount = total / once
    19. let countOfRender = 0
    20. let ul = document.querySelector("ul");
    21. function add() {
    22. // 优化性能,插入不会造成回流
    23. const fragment = document.createDocumentFragment();
    24. for (let i = 0; i < once; i++) {
    25. const li = document.createElement("li");
    26. li.innerText = Math.floor(Math.random() * total);
    27. fragment.appendChild(li);
    28. }
    29. ul.appendChild(fragment);
    30. countOfRender += 1;
    31. loop();
    32. }
    33. function loop() {
    34. if (countOfRender < loopCount) {
    35. window.requestAnimationFrame(add);
    36. }
    37. }
    38. loop();
    39. }, 0);
    40. </script>
    41. </body>
    42. </html>