解决:
// data为所有数据的数组 , callback 拿到当数据要做的操作 ,pageSize 一次取多少条 export function UtilHandleBigData(data, callback, pageSize = 100) { let totalCount = data.length; // 共多少条 let currentPageNumber = 1; // 当前页数 let totalPageNumer = Math.ceil(totalCount / pageSize); //可分多少页,就是分割为多少个小数组 let handler = () => { let start = (currentPageNumber - 1) * pageSize; let end = currentPageNumber * pageSize; let currentData = data.slice(start, end); // 当前页的数据 if (typeof callback === "function") { callback(currentData, { totalCount, totalPageNumer, currentPageNumber, pageSize, }); } // console.log( // `共${totalCount}条,共${totalPageNumer}页,当前第${currentPageNumber}条` // );
// 未执行完,继续执行
if (currentPageNumber < totalPageNumer) { window.requestAnimationFrame(handler); } currentPageNumber++; }; handler(); }
调用:
let allData = [0, 1, 2 /**...很多条*/]; // data 为分割的小数组 UtilHandleBigData(allData, data => { // do something... });
原文:https://www.cnblogs.com/Console-LIJIE/p/15343413.html