VUE 防抖与节流

// 防抖函数
export function debounce(fn, delay){
  let timeout = null
  return function(){
    let args = arguments
    if(timeout) window.clearTimeout(timeout)
    timeout = setTimeout(() => {
      fn.apply(this, args)
    }, delay)
  }
}
// 节流函数
export function throttle(fn, wait){
  let timeout = null
  return function(){
    let args = arguments
    if(!timeout){
      timeout = setTimeout(() => {
        timeout = null
        fn.apply(this, args)
      }, delay)
    }
  }
}

// 事件调用
methods: {
  handleClick(){
    this.debounce(this)
},
  debounce: debounce((vm) => {
  // do something,这里this不指向Vue实例,用vm传入
  }, 1000),
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容