You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** * * @param {*} fn :callback function * @param {*} duration :duration time,default wait time 0.8 秒 * @demo in vue methods: * handleEvent: _debounce(function(){ * do something * },time) */exportconst_debounce=(fn,duration=800)=>{// create timerlettimer=nullreturnfunction(){// reset once callclearTimeout(timer)// create a new timer to make sure call after define timetimer=setTimeout(()=>{// execute callbak, the 2nd params is fn's argumentsfn.apply(this,arguments)},duration)}}
节流函数
触发高频事件后,在n秒内只会执行一次,所以节流会稀释函数的执行频率
函数节流是固定时间做某一件事,比如每隔1秒发一次请求
监听 滚动条加载更多,频繁查询
/** * @param {*} fn: callback function * @param {*} duration : duration time,default wait time 1 sec * @demo in vue methods: * handleEvent: _throttle(function(){ * do something * },time) */exportconst_throttle=(fn,duration=1000)=>{letcanRun=truereturnfunction(){if(!canRun)returncanRun=false// execute callbak, the 2nd params is fn's argumentsfn.apply(this,arguments)setTimeout(()=>{canRun=true},duration)}}
防抖是将多次执行变为只执行一次,节流是将多次执行变为每隔一段时间执行
函数防抖是某一段时间内只执行一次,而函数节流是间隔时间执行
The text was updated successfully, but these errors were encountered:
防抖函数
节流函数
防抖是将多次执行变为只执行一次,节流是将多次执行变为每隔一段时间执行
函数防抖是某一段时间内只执行一次,而函数节流是间隔时间执行
The text was updated successfully, but these errors were encountered: