每帧渲染一个组件,写一个方法(默认设置一下监听的最大渲染帧数,超过最大渲染帧数,不再执行当前方法),方法中记录一下当前是第几帧,返回一个方法,接收的参数为要在第几帧渲染,此方法返回当前帧数是否大于目标帧数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function useDefer(frameCount = 1000){
let count = 1;
function refreshFrame(){
requestAnimationFrame(() => {
count++
if(count < frameCount){
refreshFrame();
}
})
}
refreshFrame();
return function(showInFrameCount){
return count > showInFrameCount
}
}

在vue和react中需要把count改为响应式变量,否责不会重新渲染

感觉这个和分块执行任务差不多