vue封装手机验证码

// 获取验证码
let endMsRes = new Date().getTime() + 45000;
localStorage.setItem("myEndTime", JSON.stringify(endMsRes));
this.codeCountDown(endMsRes);

// 存储验证码时隔
    codeCountDown(endMsRes) {
      this.BtnStatus = false;
      this.countDownTime = Math.ceil((endMsRes - new Date().getTime()) / 1000);
      let time = setTimeout(() => {
        this.countDownTime--;
        if (this.countDownTime < 1) {
          this.BtnStatus = true;
          this.countDownTime = 45;
          localStorage.removeItem("myEndTime");
          clearTimeout(time);
        } else {
          this.codeCountDown(endMsRes);
        }
      }, 1000);
    },
    
 created() {
    // 判断下本地是否存在验证码时隔
    let myEndTime = localStorage.getItem("myEndTime");
    myEndTime && this.codeCountDown(myEndTime);
  },

作者:Vam的金豆之路

主要领域:前端开发

我的微信:maomin9761

微信公众号:前端历劫之路