首页 > 其他 > 详细

页面插入 10万条数据 不卡段 优化方法

时间:2020-12-02 23:25:13      阅读:67      评论:0      收藏:0      [点我收藏+]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>

<ul></ul>

<script>
// 插入 10万条数据
const total = 100000;
// 每次插入 20条数据
const once = 20;
// 插入都数据需要都次数
const loopCount = Math.ceil(total/once);

// 渲染的次数

let countRender = 0;
const ul = document.querySelector(‘ul‘);

// 添加数据的方法
function add(){
// 创建虚拟节点
const fragment = document.createDocumentFragment();
for(let i=0;i<once;i++){
const li = document.createElement(‘li‘);
li.innerHTML = Math.floor(Math.random()*100000)
fragment.appendChild(li)
}

ul.appendChild(fragment)
countRender++;
loop();
}

function loop(){
if(countRender < loopCount){
window.requestAnimationFrame(add)
}
}

loop();
</script>
</body>
</html>

页面插入 10万条数据 不卡段 优化方法

原文:https://www.cnblogs.com/eric-share/p/14076217.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!