zoukankan      html  css  js  c++  java
  • Vue 验证码倒计时实现(刷新保持状态)


    mConfigs.ver_code_ide 这个为倒计时写入stora的key唯一名称,为了避免系统内,多处倒计时,key不是唯一问题

    大家参考倒计时逻辑即可,每个人的项目不同,这里提供大概的实现代码

    export default {
      name: "bm_phone_login",
      data () {
        return {
          //用户输入信息
          input_info: {
            phone: "",
            code: "",
          },
          //倒计时
          countDownTime: 60,
          timeOut: true,
          timer: null,
          //错误信息
          errorFlag: {
            phone_empty: false,
            phone_err: false,
            code_empty: false,
            code_err: false
          },
          //登录按钮禁用
          isLoginForbidden: true,
          // 获取验证码按钮禁用
          getCodeDisabled: true,
          // 获取验证码按钮文字
          getCodeBtnText: "获取验证码",
    
          countdownIng: false,
        }
      },
      mounted () {
        let sendEndTime = localStorage.getItem(`startTime${this.mConfigs.ver_code_ide}`);
        if (sendEndTime) {
          this.countdown();
        }
      },
    
      methods: {
        /**************************************************************获取验证码**************************************************************/
        countdown () {
          let that = this;
    
          let startTime = localStorage.getItem(`startTime${that.mConfigs.ver_code_ide}`);
          let nowTime = new Date().getTime();
          if (startTime) {
            let surplus = 60 - parseInt((nowTime - startTime) / 1000, 10);
            this.countDownTime = surplus <= 0 ? 0 : surplus;
          } else {
            this.countDownTime = 60;
            localStorage.setItem(`startTime${this.mConfigs.ver_code_ide}`, nowTime);
          }
    
          that.timer = setInterval(() => {
            that.countDownTime--;
            that.getCodeBtnText = that.countDownTime + "秒";
            that.getCodeDisabled = true;
            that.countdownIng = true;
            if (that.countDownTime <= 0) {
              localStorage.removeItem(`startTime${this.mConfigs.ver_code_ide}`);
              clearInterval(that.timer);
              that.countDownTime = 60;
              that.timeOut = true;
              that.countdownIng = false;
              if (that.input_info.phone == "") {
                that.getCodeDisabled = true;
              } else {
                that.getCodeDisabled = false;
              }
              that.getCodeBtnText = "获取验证码";
            }
          }, 1000)
        },
        getVerifyCode () {
          let that = this;
          let input_phone = this.input_info.phone.replace(/\s*/g, "");      //去除空格
          this.$emit('parent_get_code', input_phone, function (flag) {
            if (flag) {
              //倒计时
              if (!that.errorFlag.phone_err && !that.errorFlag.phone_empty) {
                // Toast('验证码已发送');
                that.countdown();
              } else {
                that.errorFlag.phone_empty = true;
              }
            } else {
              that.timeOut = true;
            }
          });
          this.timeOut = false;
    
       }
    }
    </script>
    
    作者:GG
    本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
  • 相关阅读:
    dalvik浅析二:jni、so
    android apk壳
    prism4 StockTrader RI 项目分析一些体会
    PRISM ‘VS100COMNTOOLS’ not set. Cannot set the build environment
    安卓移动应用代码安全加固系统设计及实现
    手机游戏外挂技术研究与运用
    移动终端安全威胁与应对措施
    游戏软件反外挂技术方案的设计和安全解决方案
    网络安全等级保护 2.0 下的安全体系建设
    APP加密,ios代码混淆工具,虚拟化技术 适用于移动应用程序的虚拟化加密软件
  • 原文地址:https://www.cnblogs.com/codedisco/p/15681824.html
Copyright © 2011-2022 走看看