验证码倒计时模板

<template>
    <view class="wai">
        <view class="box-phone">
            <view class="">手机号</view>
            <input class="input" placeholder="请输入手机号" type="text" />
        </view>
        <view class="box-phone">
            <view class="">验证码</view>
            <input class="input" placeholder="请输入验证码" type="text" />
            <view class="code" @click="getVerificationCode">{{ isCounting ? countdownText : '获取验证码' }}</view>
        </view>
        <view class="box-phone">
            <view class="">新密码</view>
            <input class="input" placeholder="请设置新密码" type="password" />
        </view>
        <view class="box-phone">
            <view class="">确认密码</view>
            <input class="input2" placeholder="请再次输入新密码" type="password" />
        </view>
        <!-- 按钮 -->
        <view class="btn">确定</view>
    </view>
</template>

<script setup>
import { ref, onMounted, onUnmounted, computed } from 'vue';
const countdown = ref(5); //倒计时
const isCounting = ref(false); //多次点击只执行一次,控制显示秒数
let timer = null;
// 计算倒计时的文本
const countdownText = computed(() => {
    if (isCounting.value) {
        return `${countdown.value}s重新获取`;
    }
});
// 点击获取验证码
const getVerificationCode = async () => {
    if (isCounting.value) return; // 如果已经在倒计时,则不执行
    isCounting.value = true; // 开始倒计时
    startCountdown();
};
// 倒计时函数
const startCountdown = () => {
    timer = setInterval(() => {
        if (countdown.value > 1) {
            //如果秒数大于零就--
            countdown.value--;
        } else {
            //如果不大于零就移除定时器,其他数据复原
            clearInterval(timer);
            isCounting.value = false; //结束倒计时
            countdown.value = 5; //秒数复原
        }
    }, 1000);
};
// 移除定时器
const resetCountdown = () => {
    clearInterval(timer);
    isCounting.value = false;
    countdown.value = 60;
};
onUnmounted(() => {
    // 组件卸载时清除定时器
    resetCountdown();
});
</script>

<style lang="scss" scoped>
.wai {
    .btn {
        background-color: #f8bf5a;
        width: 100%;
        height: 90rpx;
        text-align: center;
        line-height: 90rpx;
        color: #fff;
        border-radius: 50rpx;
        margin-top: 60rpx;
    }
    .box-phone {
        .code {
            border-radius: 50rpx;
            color: #fff;
            background-color: #34d19d;
            padding: 20rpx;
            font-size: 30rpx;
        }
        .input2 {
            flex: 1;
            margin-left: 48rpx;
        }
        .input {
            flex: 1;
            margin-left: 76rpx;
        }
        height: 120rpx;
        border-bottom: 2rpx solid #f7f7f7;
        display: flex;
        align-items: center;
    }
    padding: 0 30rpx;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值