javascript
/**
* 延迟批量上报数据
*
* @param data 需要上报的数据
*/
export function lazyReportBatch(data: any) {
addCache(data);
const dataCache = getCache();
const reportData = async () => {
if (!dataCache.length) {
return;
}
sendServe(dataCache);
clearCache();
};
if (dataCache.length && dataCache.length > config.batchSize) {
reportData();
} else {
// 游览器空闲时机上报
if ('requestIdleCallback' in window) {
window.requestIdleCallback(reportData, { timeout: 1000 });
} else {
setTimeout(reportData, 1000);
}
}
}