倒计时60s的使用

  

 <button @click="send" :disabled="disabled">{{yzm}}</button>
 data() {
    return {
      yzm: "发送验证码",
      count: 0,
      interval: undefined,
      disabled: false
    };
  },
 send() {
//定义60s
      this.count = 60;
//点击后将按钮禁用
      this.disabled = true;
//设置定时器
      this.interval = setInterval(() => {
        this.count--;
//判断时间并把数据写入内容
        this.yzm = this.count !== 0 ? `${this.count}秒再次获取` : "获取验证码";
        if (this.count === 0) {
//时间为零时关闭定时器
          clearInterval(this.interval);
//取消开关禁用
          this.disabled = false;
        }
      }, 1000);
 

猜你喜欢

转载自www.cnblogs.com/sanjinlizi/p/12463717.html