小程序 验证码60秒倒计时

本文介绍了一种在前端页面中实现验证码倒计时功能的方法。通过设置初始时间和禁用按钮的状态来控制用户获取验证码的行为,并使用 setInterval 实现了倒计时效果。当倒计时结束后,按钮将重新启用并允许用户再次获取验证码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<button disabled="{{isClick}}" class="r_code" bindtap="getCountdown">{{timeCode}}</button>
 /**
     * 页面的初始数据
     */
    data: {
        navbarData: { // 组件所需的参数
            showCapsule: true, //是否显示左上角图标  
            capsuleType: 'return', // 左上角图标类型  / return
            title: '用户注册', //导航栏 中间的标题
        },
        timeCode: '获取验证码',
        currentTime: 61,  // 验证码倒计时秒数
        isClick: false, // 获取验证码是否能点击
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },
    /**
     * 获取验证码 倒计时60秒
     */
    getCode() {
        let that = this;
        let currentTime = that.data.currentTime
        let interval = setInterval(() => {
            currentTime = currentTime - 1
            that.setData({
                timeCode: currentTime + '秒'
            })
            if (currentTime <= 0) {
                clearInterval(interval)
                that.setData({
                    timeCode: '重新发送',
                    currentTime: 61,
                    isClick: false
                })
            }
        }, 1000)
    },
    /**
     * 点击 获取验证码
     */
    getCountdown() {
        let that = this;
        this.getCode();
        that.setData({
            isClick: true
        })
    },

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值