lengziyu / noteBook

This is my noteBook.
0 stars 0 forks source link

发送手机验证码 #12

Open lengziyu opened 7 years ago

lengziyu commented 7 years ago
    //验证码倒计时时间
    var countdown = 60;
    var maxtime = 60;                      
    var authCodeEl = '#authCode'   //点击获取验证码元素
    //获取缓存时间
    function getlocalTime(str){
      var getLocal = localStorage.getItem(str);
      console.log(getLocal)
      if(getLocal != maxtime){
        countdown = getLocal;
        invokeSettime(authCodeEl);
      }
    }
    getlocalTime('sendCount');

    //设置验证码倒计时
    function invokeSettime(obj) {
      settime(obj);
      function settime(obj) {
        if (countdown == 0) {
          $(obj).removeClass('btn-disabled');
          $(obj).html("重新验证码");
          countdown = maxtime;
          return;
        } else {
          $(obj).addClass('btn-disabled');
          $(obj).html(countdown + "s 重新发送");
          countdown--;
        }
        setTimeout(function() {
          settime(obj)
          window.localStorage.setItem('sendCount', countdown)
        }, 1000)
      }
    }

    //点击获取验证码
    $(document).on('click', authCodeEl, function() {
     //倒数计时的时候,防止第二次点击导致验证码的错乱情况
      if(countdown == maxtime){
        invokeSettime(authCodeEl);
      }

使用localStorage解决刷新或关闭窗口之后重置倒计时问题。