首页 > 其他分享 >vue中防抖函数的写法以及用法

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

时间:2022-09-19 15:58:14浏览次数:59  
标签:function vue log 中防抖 args timeout 写法 methods

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);
    }
  },

 

标签:function,vue,log,中防抖,args,timeout,写法,methods
From: https://www.cnblogs.com/web-aqin/p/16707902.html

相关文章