cleanTimeout没有起作用
发布于 3 年前 作者 huaer 1212 次浏览 来自 问答
粉丝福利 : 关注VUE中文社区公众号,回复视频领取粉丝福利
问题: vue项目中这是一个立即执行版本的防抖函数,
但是为什么注释处打印出来的timeout不是undefined或者 null,
也就是说cleanTimeout好像没有起作用,求教

  debounce(fn: any, wait: number) {
    let timeout: any;
    return function (this: any) {
      let self = this
      let arg = arguments
      console.log(timeout)
      if (timeout) {
        console.log('haha')
        clearTimeout(timeout)
        console.log(timeout)   //  这个地方有问题 
      }
      console.log(timeout)
      let callnow = !timeout
      timeout = setTimeout(() => {
        timeout = null
      }, wait);
      if (callnow) fn.apply(self, arg);
    };
  }
回到顶部