vue 验证码倒计时读秒

<div style="margin-top: 15px;">
          <el-input placeholder="请输入验证码" v-model="code">
            <template slot="append">
              <el-button
                class="code-btn"
                :disabled="isDisbaled"
                @click="sendCode"
              >
                {{ codeBtnContent }}
              </el-button>
            </template>
          </el-input>
        </div>
    countdown: 60,
      codeBtnContent: "发送验证码",
      isDisbaled: false,
    sendCode() {
      // 调研延迟读秒函数
      this.setTime();
    },
    // 延时器倒计时
    setTime() {
      if (this.countdown == 0) {
        this.isDisbaled = false;
        this.codeBtnContent = "发送验证码";
        this.countdown = 60; //60秒过后button上的文字初始化,计时器初始化;
        return;
      } else {
        this.isDisbaled = true;
        this.codeBtnContent = this.countdown + "s";
        this.countdown--;
      }
      setTimeout(() => {
        this.setTime();
      }, 1000); //每1000毫秒执行一次
    },

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中实现验证码倒计时功能的关键是使用定时器来更新倒计时的时间,并根据时间的变化来控制按钮的状态和显示文本。以下是一个实现验证码倒计时功能的Vue代码示例: ```html <template> <button :disabled="isSend" @click="countDown">{{ codeName }}</button> </template> <script> export default { data() { return { isSend: false, codeName: "发送验证码", totalTime: 10, timer: null }; }, methods: { countDown() { if (this.isSend) return; this.isSend = true; this.codeName = this.totalTime + 's后重新发送'; this.timer = setInterval(() => { this.totalTime--; this.codeName = this.totalTime + 's后重新发送'; if (this.totalTime < 0) { clearInterval(this.timer); this.codeName = '重新发送验证码'; this.totalTime = 10; this.isSend = false; } }, 1000); } } }; </script> ``` 在上述代码中,我们使用了一个`isSend`变量来控制按钮的禁用状态,`codeName`变量来控制按钮的显示文本,`totalTime`变量来表示倒计时的总时间,`timer`变量来保存定时器的引用。 在`countDown`方法中,我们首先判断如果已经发送过验证码,则直接返回。然后将`isSend`设置为`true`,将`codeName`设置为倒计时的初始文本。接着使用`setInterval`函数创建一个定时器,每隔1秒更新一次倒计时的时间和按钮的显示文本。当倒计时结束时,清除定时器,将`codeName`恢复为重新发送的文本,将`totalTime`重置为初始值,将`isSend`设置为`false`。 这样,当用户点击按钮时,就会触发倒计时功能,按钮会进入禁用状态,并显示倒计时的时间。当倒计时结束后,按钮恢复为可点击状态,并显示重新发送的文本。 #### 引用[.reference_title] - *1* [vue实现验证码倒计时](https://blog.csdn.net/qq_41793354/article/details/121910500)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [vue实现验证码倒计时功能](https://blog.csdn.net/weixin_48168510/article/details/119873604)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值