美文网首页
VUE 防抖与节流

VUE 防抖与节流

作者: 小碗吃不了 | 来源:发表于2021-07-06 14:28 被阅读0次
// 防抖函数
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),
}

相关文章

网友评论

      本文标题:VUE 防抖与节流

      本文链接:https://www.haomeiwen.com/subject/rumqeltx.html