vue 一个页面多个短信验证倒计时方法

1. 创建form表单

<template>
<div class="phonebox1">
  <el-form :model="MailboxForm" :rules="Mailboxrules" ref="MailboxForm" class="demo-Form">
    <el-form-item prop="editcode" class="pr">
      <el-input v-model="MailboxForm.editcode" placeholder="请输入验证码"></el-input>
      <el-button :disabled="!countdownList[0].BtnStatus" @click="obtainmail1(0)" class="code-btn">
        {{ countdownList[0].BtnStatus ? "获取验证码" : `${countdownList[0].countDownTime}秒后重新获取` }}
      </el-button>
    </el-form-item>
    <el-form-item prop="codes" class="pr">
      <el-input v-model="MailboxForm.codes" placeholder="请输入验证码"></el-input>
      <el-button :disabled="!countdownList[1].BtnStatus" @click="obtainmail2(1)" class="code-btn">
        {{ countdownList[1].BtnStatus ? "获取验证码" : `${countdownList[1].countDownTime}秒后重新获取` }}
      </el-button>
    </el-form-item>
  </el-form>
   <span slot="footer" class="dialog-footer">
     <el-button type="primary" @click="deletMail" size="small">提 交</el-button>
   </span>
</div>
</template>

2.  初始化变量

 data () {
  return {
  MailboxForm: {
    editcode: "",
    codes: "",
  },
  Mailboxrules: {
    editcode: [{ required: true, message: "验证码不能为空", trigger: "blur" }],
    codes: [{ required: true, message: "验证码不能为空", trigger: "blur" }]
  },
  countdownList: [
   {
    countDownTime: 60,//倒计时时间
    BtnStatus: true,//按钮的启用状态
    name: "oldmail"
   },
    {
    countDownTime: 60,//倒计时时间
    BtnStatus: true,//按钮的启用状态
    name: "newmail"
   }
  ],
 };
},

 3. 获取localStorage保存的倒计时时间,初始化倒计时方法

created () {
    //初始化
    this.countdownList.forEach((item, index) => {
      let myEndTime = localStorage.getItem(item.name);
      myEndTime && this.codeCountDown(myEndTime, index);
    });
  },

 4. 倒计时方法

 methods: {
     //倒计时方法
    codeCountDown (endMsRes, index) {
      this.countdownList[index].BtnStatus = false;//设置el-button 的禁用状态
      this.countdownList[index].countDownTime = Math.ceil((endMsRes - new Date().getTime()) / 1000); //剩余多少秒
      let time = setTimeout(() => {
        this.countdownList[index].countDownTime--;
        if (this.countdownList[index].countDownTime < 1) {
          this.countdownList[index].BtnStatus = true;
          this.countdownList[index].countDownTime = 60;
          localStorage.removeItem(this.countdownList[index].name);//清除localStorage
          clearTimeout(time);
        } else {
          this.codeCountDown(endMsRes, index);
        }
      }, 1000);
    },
      //点击事件
    obtainmail1 (index) {
        let endMsRes = new Date().getTime() + 60000; //当前时间戳加上一分钟的时间戳,相当于当前时间一分钟以后的时间戳
        localStorage.setItem("oldmail", JSON.stringify(endMsRes));//localStorage 设置倒计时时间
        this.codeCountDown(endMsRes, index);
    },
       //点击事件
    obtainmail2 (index) {
        let endMsRes = new Date().getTime() + 60000;
        localStorage.setItem("newmail", JSON.stringify(endMsRes));
        this.codeCountDown(endMsRes, index);
    },
    //提交表单清除localStorage保存的倒计时时间
    deletMail () {
        localStorage.removeItem("oldmail");
        localStorage.removeItem("newmail");
    }
 }

5. 样式

<style lang="less" scoped>
.phonebox1 {
  .pr {
    position: relative;
  }
  .code-btn {
    width: 130px;
    height: 34px;
    position: absolute;
    top: 3px;
    right: 5px;
    z-index: 222;
    color: #f5a623;
    font-size: 14px;
    border: none;
    border-left: 1px solid #bababa;
    padding-left: 16px;
    background-color: #fff;
    cursor: pointer;
  }
  .nextstep {
    width: 100%;
    height: 30px;
    background: red;
    color: white;
    border-radius: 5px;
    cursor: pointer;
  }
}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过Vue的计时器和条件渲染实现一个简单的登录验证60秒倒计时。以下是一个示例代码: ```html <template> <div> <form @submit.prevent="handleSubmit"> <label> 用户名: <input type="text" v-model="username" /> </label> <label> 密码: <input type="password" v-model="password" /> </label> <button :disabled="isCountingDown">{{ buttonText }}</button> </form> <div v-if="isCountingDown"> <p>请稍等{{ countDown }}秒...</p> </div> </div> </template> <script> export default { data() { return { username: "", password: "", countDown: 60, isCountingDown: false, timer: null, }; }, computed: { buttonText() { return this.isCountingDown ? `请稍等${this.countDown}秒...` : "登录"; }, }, methods: { handleSubmit() { // 处理登录逻辑 if (this.username === "admin" && this.password === "password") { alert("登录成功"); } else { alert("用户名或密码错误"); this.startCountDown(); } }, startCountDown() { this.isCountingDown = true; this.timer = setInterval(() => { this.countDown--; if (this.countDown <= 0) { clearInterval(this.timer); this.isCountingDown = false; this.countDown = 60; } }, 1000); }, }, }; </script> ``` 在上面的代码中,我们通过 `isCountingDown` 属性来控制倒计时的渲染,使用计时器来实现倒计时的计算和更新。在登录失败时,我们可以调用 `startCountDown` 方法来触发倒计时的开始。登录成功时,我们不需要做任何处理,计时器会在60秒后自动停止。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值