微信小程序----短信验证码倒计时插件

本文介绍了一个用于微信小程序的验证码倒计时插件的实现方法,包括插件的使用步骤及注意事项,适用于希望优化用户体验的开发者。

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

WXRUI体验二维码

WXRUI体验码

如果文章对你有帮助的话,请打开微信扫一下二维码,点击一下广告,支持一下作者!谢谢!

效果图

短信验证码倒计时插件演示图

JS

// 倒计时插件
class CountTime {
  constructor(page) {
    this.page = page;
    this.time = 60;
    this.timer = null;
    this.page.setData({ code: '获取验证码', flag: false });
  }
  countTime() {
    this.page.setData({ flag: true });
    if (this.time > 0) {
      this.time--;
      this.page.setData({ code: this.time + 's后获取' });
      this.timer = setTimeout(() => {
        this.countTime();
      }, 1000);
    } else {
      this.time = 60;
      clearTimeout(this.timer);
      this.page.setData({ code: '获取验证码', flag: false });
    }
  }
}
module.exports = CountTime;

使用方法

1.引入插件countTime.js

const CountTime = require("../../utils/countTime.js");

2.在 onLoad 周期初始化

this.time = new CountTime(this);

3.在点击获取二维码按钮中使用

// 调用验证码获取倒计时方法
getCode(){
    let phoneCodeVerification = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (phoneCodeVerification.test(this.data.phone)){
        if (!this.data.flag) {
        this.time.countTime();
        // 获取验证码
        this.getCodeData();
        }else{
        this.wetoast.toast({ title: '请不要急躁,60s后再次获取!' });
        }
    }else{
        this.wetoast.toast({ title: '手机号码格式不正确,请重新输入!'});
    }
}

4.完整使用方法

const CountTime = require("../../utils/countTime.js");
const urlList = require("../../utils/config.js");
const app = getApp();
Page({
  onLoad(){
    this.time = new CountTime(this);
    // 构建WeToast
    this.wetoast = new app.WeToast();
  },
  // 获取手机号码
  getPhone(e){
    this.setData({phone: e.detail.value})
  },
  // 获取验证码
  getCodeData() {
    app.globalMethod.POST({
      url: urlList.getModifyPersonalInfoCodeUrl,
      data: { phone: this.data.phone, type: 1 },
      success: res => {
        // console.log(res)
        if (res.data.code == "200") {
          this.wetoast.toast({ title: '验证码发送成功,请注意查收!' });
        } else {
          this.wetoast.toast({ title: res.data.message });
        }
      }
    })
  },
  // 调用验证码获取倒计时方法
  getCode(){
    let phoneCodeVerification = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (phoneCodeVerification.test(this.data.phone)){
      if (!this.data.flag) {
        this.time.countTime();
        // 获取验证码
        this.getCodeData();
      }else{
        this.wetoast.toast({ title: '请不要急躁,60s后再次获取!' });
      }
    }else{
      this.wetoast.toast({ title: '手机号码格式不正确,请重新输入!'});
    }
  }
})

注意

  1. 在进行倒计时前需要对手机号进行判断,验证手机号码是否正确的正则
  2. 判断 flag 的值,防止多次点击,进行多次求情。
  3. 执行倒计时后在执行获取二维码请求的函数。

优化

  1. 按钮文字、倒计时时间、可以进行自定义使用传入值。
  2. 将倒计时不能多次点击的判断放入插件内部,调用插件直接倒计时。
  3. 防止插件没执行成功就执行了获取验证码函数,应该再建立一个插件执行的成功函数,将获取验证码的步骤放入成功函数中!

WXRUI体验二维码

WXRUI体验码

如果文章对你有帮助的话,请打开微信扫一下二维码,点击一下广告,支持一下作者!谢谢!

下载

我的博客,欢迎交流!

我的优快云博客,欢迎交流!

微信小程序专栏

前端笔记专栏

微信小程序实现部分高德地图功能的DEMO下载

微信小程序实现MUI的部分效果的DEMO下载

微信小程序实现MUI的GIT项目地址

微信小程序实例列表

前端笔记列表

游戏列表

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rattenking

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值