Vue3 验证码倒计时实现

2 篇文章 0 订阅

在登录界面中实现短信验证的倒计时功能

最近在学习Vue3,写登录界面时遇到了短信验证倒计时的问题,代码是根据Vue3+Antd+Js实现的,话不多说直接上代码。

<template>
  <div class="forget">
    <div class="forget-wrap">
      <a-form
        id="components-form-demo-normal-login"
        class="login-form"
        v-bind="formItemLayout"
      >
        <a-from-item>
          <!-- <span class="from-title">登录界面</span> -->
        </a-from-item>
        <a-form-item>
          <label class="from-text">账号</label>
          <a-input placeholder="请输入账号"> </a-input>
        </a-form-item>
        <a-form-item>
          <label class="from-text">新密码</label>
          <a-input type="password" placeholder="请输入新的密码"> </a-input>
        </a-form-item>
        <a-form-item>
          <label class="from-text">确认密码</label>
          <a-input type="password" placeholder="请确认新的密码"> </a-input>
        </a-form-item>
        <a-from-item>
          <label class="from-text">验证码</label>
          <a-row :gutter="10">
            <a-col :span="12">
              <a-input placeholder="请输入验证码" />
            </a-col>
            <a-col :span="12" :push="4">
              <a-button type="primary"
                ><span v-show="show" @click="Verification">获取验证码</span>
                <span v-show="!show" class="count"
                  >{{ count }} s 后获取</span
                ></a-button
              >
            </a-col>
          </a-row>
        </a-from-item>
        <a-form-item>
          <a-button
            type="primary"
            @click="submitModify"
            class="login-form-button"
          >
            找回
          </a-button>
        </a-form-item>
      </a-form>
      <div class="forget-wrap-bottom">
        <a href="" class="text-color"
          ><i class="iconfont icon-zhuce"></i> 返回登录
        </a>
        |
        <router-link to="/Register" class="text-color"
          ><i class="iconfont icon-wangjimima"></i> 快速注册
        </router-link>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from "vue";
export default {
  name: "Forget",
  setup() {
    const show = ref(true);
    const timer = ref();
    const TIME_COUNT = ref(60);
    const count = ref();
    const submitModify = () => {
      alert("修改密码成功");
    };
    //点击发送验证码
    const Verification = () => {
      if (!timer.value) {
        count.value = TIME_COUNT.value;
        show.value = false;
        timer.value = setInterval(() => {
          if (count.value > 0 && count.value <= TIME_COUNT.value) {
            count.value--;
            console.log("count----", count);
          } else {
            show.value = true;
            clearInterval(timer.value);
            timer.value = null;
          }
        }, 1000);
      }
      alert("已发送,请注意查收");
    };
    return {
      submitModify,
      Verification,
      show,
      timer,
      TIME_COUNT,
      count,
    };
  },
};
</script>

最终的结果:
在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值