0
点赞
收藏
分享

微信扫一扫

vue中防抖函数,element select组件

何晓杰Dev 2022-04-24 阅读 57
vue.js前端

目录

前言

一、vue中防抖函数

在这里插入图片描述

//防抖函数(select改变)
debounce(func, wait = 3000, immediate = false) {
  // 清除定时器
  if (this.timeout !== null) clearTimeout(this.timeout);
  // 立即执行,此类情况一般用不到
  if (immediate) {
    var callNow = !this.timeout;
    this.timeout = setTimeout(function () {
      this.timeout = null;
    }, wait);
    if (callNow) typeof func === "function" && func();
  } else {
    // 设置定时器,当最后一次操作后,timeout不会再被清除,所以在延时wait毫秒后执行func回调方法
    this.timeout = setTimeout(function () {
      typeof func === "function" && func();
    }, wait);
  }
},

使用

changeSeList(val) {  
  let fn = async () => {
    this.getSeList(); //这个方法是获取数据的  
  };
  this.debounce(fn, 3000); //注意 这里直接传入this.getSeList() 不起作用
},

在这里插入图片描述

举报

相关推荐

0 条评论