vue中防抖函数的写法以及用法

1.准备好防抖函数

function debounce(func, wait) {
      let timeout;
      return function (...args) {
        if (timeout) clearTimeout(timeout);
          let isTime = !timeout;
          timeout = setTimeout(function () {
            timeout = null;
          }, wait);
          if (isTime) func.apply(this, args);
 
      };
    }
2.html节点
<input type="button" @click="toDoSth" />
 
3.methods中调用
methods: {
    toDoSth: debounce(
      function () {
        this.log();
      },
      500,
      true
    ),
    log() {
      console.log(12313);
    }
  },
posted @ 2022-07-19 21:26  关关大大  阅读(890)  评论(0编辑  收藏  举报