前端短信验证码倒计时

  var btnSendCode = document.getElementById('btnSendCode')
  function getcode() {
    let username = $("input[name=username]").val();
    var loading = layer.load(1);
    $.post("{:url('frontend/Sign/sendCode')}", { username: username }, function (response) {
      layer.close(loading);
      if (response.code != 1) {
        layer.msg(response.msg, function () { });
        return;
      }
      layer.msg(response.msg);
      btnSendCode.setAttribute("disabled", true);
      settime(60)
    })
 
  }

  下面是倒计时的代码

  function settime(countdown) {
    if (countdown == 0) {
      btnSendCode.removeAttribute("disabled");
      btnSendCode.value = "获取验证码";
    } else {
      btnSendCode.setAttribute("disabled", true);
      btnSendCode.value = "重新发送(" + countdown + ")";
      countdown--;
    }
    setTimeout(function () {
      settime(countdown)
    }, 1000)
  }

 

posted @ 2022-11-24 17:37  潇宸1998  阅读(164)  评论(0编辑  收藏  举报