Button节流、防抖

一、组件自带

简单粗暴的先看下自己所用组件是否提供节流api,譬如为uniapp而生的u-view组件。

<template>
  <div>
    // uview@1x版本
    <u-button :throttle-time="3000">
      提交
    </u-button>

    // uview@2x版本
    <u-button :throttleTime="3000">
      提交
    </u-button>
  </div>
</template>

二、动态绑定组件loading或disabled

这里只列举element-ui中的el-button组件的loading。

<template>
  <div>
    <!-- 使用v-bind:loading绑定data选项内的btnLoading -->
    <el-button :loading="btnLoading" @click="submit">
      提交
    </el-button>
  </div>
</template>

<script>
// 模拟请求接口
import { sendMsg } from "@/api/xxx.js";

export default {
  data() {
    return {
      btnLoading: false
    }    
  },
  methods: {
    async submit() {
      // 点击button按钮时,让loading动作出现,此时button是禁用点击的
      this.btnLoading = true;
      try {
        let res = await sendMsg();
        if(res && res.code === 200) {
          // 在接口成功时,将loading动作取消,以达到节流效果
          this.btnLoading = false;
          return;
        } else {
          // 在接口失败时,不要忘记将loading动作取消
          this.btnLoading = false;
          return;
        }
      } catch(err) {
        console.log(err);
        // 接口错误时也要做相应的操作
        this.btnLoading = false;
        return;
      };
    }
  }
}
</script>

 三、自己写JS或方法类

3.1、节流

<template>
  <div>
    <button @click="submit">
      提交
    </button>
  </div>
</template>

<script>
// 模拟请求接口
import { sendMsg } from "@/api/xxx.js";

export default {
  data() {
    return {
      stopClick: false,
      timer: null
    }    
  },
  destroyed() {
    clearTimeout(this.timer);
  },
  methods: {
    submit() {
      // 如果参数为true,直接退出方法,如果为假则往下走
      if(this.stopClick) {
        console.log("1s之内只能点击1次");
        return;
      };
      // 将参数设置为true
      this.stopClick = true;
      // 设置定时器,1s后才能点击
      this.timer = setTimeout(() => {
        this.stopClick = false;
      }, 1000);
      // 做一些请求或其他业务逻辑
      this.sendMsg();
    },
    async sendMsg() {
      try {
        ...
      } catch(e) {
        ...
      };
    }
  }
}
</script>

3.2、防抖

<template>
  <div>
    <van-button
      :loading="bindBtnLoading"
      @click.stop="handleClick"
    >
      <span>绑定新学生</span>
    </van-button>
  </div>
</template>

<script>
  data() {
    return {
      bindBtnLoading: false,
      timer: null
    }
  },
  method: {
    handleClick() {
      this.bindBtnLoading = true
      if (this.timer) {
        clearTimeout(this.timer)
      }
      this.timer = setTimeout(() => {
        this.getData()
      }, 300)
    },
    async getData() {
      try {
        cosnt res = await getxxx()
        if (!res) {
          this.bindBtnLoding = false
          // ...
          return
        }
        this.bindBtnLoding = false
        // ...
        return
      } catch(e) {
        this.bindBtnLoding = false
        // ...
      }
    }
  }
</script>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值