防抖节流实现

1.防抖

function debounce(fun, time) {
  let timers;
  return function(){
    clearTimeout(timers);
    let arg = arguments;
    timers = setTimeout( () => {
      fun.apply(this, arg)
    },time)
  }
}

2.节流

function throttle(fun,time){
  let t1=0 //初始时间
  return function(){
    let t2=new Date() //当前时间
    if(t2-t1>time){
      fun.apply(this,arguments)
      t1=t2
    }
  }
}
原文章:https://juejin.cn/post/7016502001911463950
posted @ 2022-07-05 19:23  不知码  阅读(19)  评论(0)    收藏  举报