// Some code
// Some code
const throttleByAnimationFrame = (func) => {
let callArgs = null
const frameFunc = () => {
const currentCallArgs = callArgs
callArgs = null
func.apply(null, currentCallArgs)
}
return (...args) => {
!callArgs && requestAnimationFrame(frameFunc) //防止一个渲染进程中多次注册
callArgs = args
}
}
var sayNum = throttleByAnimationFrame((a,b,c)=>{console.log(a,b,c)})
当触发时会判断是否上一个requestAnimationFrame调用还在执行,存在的话抛弃掉. 更新requestAnimationFrame实际传参,然后等待下一个浏览器重绘时间根据此参数执行.
// Some code
const throttleByAnimationFrame = (func) => {
let callArgs = null
const frameFunc = () => {
const currentCallArgs = callArgs
func.apply(null, currentCallArgs)
}
return (...args) => {
args == callArgs || requestAnimationFrame(frameFunc) //如果新来的参数和旧参数相同则抛弃
callArgs = args
}
}
var sayNum = throttleByAnimationFrame((a,b,c)=>{console.log(a,b,c)})
当触发时会判断是否已经有一个同样参数的requestAnimationFrame调用存在,存在的话抛弃掉. 不存在的话更新requestAnimationFrame实际传参,然后等待下一个浏览器重绘时间根据此参数执行.