关于节流函数
資深大佬 : rodrick 5
之前学的时候自己是这样写的
function throttle(fn, wait) { let lastTime = 0 let timer = null let nowTime = new Date().getTime(); return function (...args) { if (nowTime - lastTime > wait) { fn.apply(this, args) lastTime = nowTime } else if (timer === null) { timer = setTimeout(() => { timer = null fn.apply(this, args) }, wait) } } }
使用的时候发现如果定时器结束执行 fn.apply(this, args) 的话,那么这个 args 是当前时间减去 wait 的那个时间点的参数,并不是当前时间的参数
在一些监控滑动位置等情况时[args 是当前位置信息]可能会导致执行 fn 的时候并不是使用的最准确的位置信息,那么怎么能拿到准确时间点的参数呢
大佬有話說 (6)