微信小程序获取验证码60秒倒计时功能

 更新时间:2023年04月24日 10:27:36   作者:switch  
这篇文章主要介绍了微信小程序获取验证码60秒倒计时模板,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

效果图

index.wxml

     <view class="Info">
            <view class="Num">
                <view>6位数字验证码</view>
                <view class="verification_time">
                    <button bindtap='MosendSms' disabled='{{MoDisabled}}' class="Box_hid" style='color:{{MoColor}}' type="button">{{MoCodeMsg}}</button>
                </view>
            </view>
            <view class='verification'>
                <block wx:for="{{Length}}" wx:key="item">
                    <input class='frame' value="{{Showboxval.length>=index+1?Showboxval[index]:''}}" disabled catchtap='InputTap' />
                </block>
            </view>
            <view class="error" wx:if="{{error}}">验证码输入错误</view>
            <input name="password" password="{{true}}" class='ipt' maxlength="{{Length}}" focus="{{isFocus}}" bindinput="FocuInput" />
      </view>

index.js

           data: {
                code: "", //后端验证码
                Jurisdiction: true, //是否有权限
                error: false, //错误提示
                Length: 6, //输入框个数
                isFocus: true, //聚焦
                Showboxval: "", //输入的内容
                MoDisabled: false, //验证码是否可点击
                MoCodeMsg: '获取验证码', //文案
                MoMsgWait: 60, //时间秒
                MoColor: 'rgba(40, 200, 122, 1)', //默认验证码字体颜色
            },
             /**
             * 生命周期函数--监听页面加载 正常第一次进来先调用一次
             */
            onLoad(options) {
               this.MosendSms() // 60秒后重新获取验证码
            },
            // 60秒后验证码
            sendSbinms() {
                wx.showToast({
                    title: '短信验证码发送成功,请注意查收',
                    icon: 'none'
                })
                this.setData({
                    MoCodeMsg: this.data.MoMsgWait + "  s",
                    MoColor: 'rgba(40, 200, 122, 1)',
                    MoMsgWait: this.data.MoMsgWait - 1,
                    MoDisabled: true
                });
                let inter = setInterval(function () {
                    this.setData({
                        MoCodeMsg: this.data.MoMsgWait + "  s",
                        MoColor: 'rgba(40, 200, 122, 1)',
                        MoMsgWait: this.data.MoMsgWait - 1,
                        MoDisabled: true
                    });
                    if (this.data.MoMsgWait < 0) {
                        clearInterval(inter)
                        this.setData({
                            MoCodeMsg: "重新获取",
                            MoMsgWait: 60,
                            MoDisabled: false,
                            MoColor: 'rgba(40, 200, 122, 1)'
                        });
                    }
                    //注意后面的bind绑定,最关键。不然又是未定义,无法使用外围的变量;
                }.bind(this), 1000);
            },
            // 点击获取验证码
            MosendSms() {
               if (this.data.Jurisdiction) {
                   this.sendSbinms() // 60秒后重新获取验证码
                   this.obtain();  //后端接口 获取验证码
               } else {
                   wx.showToast({
                      title: this.data.massage ? this.data.massage : '手机号未注册',
                       icon: 'error',
                       duration: 3000
                   })
               }
             },
             obtain() {
                    let params = {
                        phone: this.data.rstProduct,
                        type: 1
                    }
                    appletValidateCode(params).then((res) => {
                        this.setData({
                            code: res.data.data,
                        });
                    }).catch((res) => {})
                },
     //验证码输入框
        FocuInput(e) {
            let that = this;
            let inputValue = e.detail.value;
            that.setData({
                Showboxval: inputValue,
            })
            if (inputValue.length === 6) {
                if (inputValue == this.data.code) {
                    this.setData({
                        error: false,
                    });
                } else {
                    this.setData({
                        error: true,
                    });
                }
            }
        },
   //验证码输入框点击
    InputTap() {
        let that = this;
        that.setData({
            isFocus: true,
        })
    },

index.wxss

.Info {
    padding: 138rpx 32rpx 0 32rpx;
}

.verification {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 30rpx;
    font-size: 32rpx;
}

.Num {
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: rgba(0, 0, 0, 0.65);
    font-size: 30rpx;
    font-family: PingFang SC-Regular, PingFang SC;
    font-weight: 400;
}

.frame {
    width: 80rpx;
    height: 80rpx;
    border-radius: 2px;
    border: 2rpx solid #DEDEDE;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
    font-size: 32rpx;
    font-family: PingFang SC-Medium, PingFang SC;
    font-weight: 500;
    color: rgba(0, 0, 0, 0.65);
}

.ipt {
    width: 0;
    height: 0;
}

.Box_hid {
    font-size: 30rpx;
    background: #fff !important;
    text-align: left;
    color: rgba(40, 200, 122, 1) !important;
    padding-right: 0 !important;
    font-family: PingFang SC-Regular, PingFang SC;
    font-weight: 400 !important;
}

.Box_hid::after {
    border: none;
}

.error {
    color: #F24236;
    margin-top: 8rpx;
    font-size: 28rpx;
    font-family: PingFang SC-Regular, PingFang SC;
    font-weight: 400;
}

到此这篇关于微信小程序获取验证码60秒倒计时模板的文章就介绍到这了,更多相关微信小程序60秒倒计时内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • JavaScript SHA512&SHA256加密算法详解

    JavaScript SHA512&SHA256加密算法详解

    本文给大家分享的是javascript版的SHA512&SHA256加密算法的代码,以及用法,有需要的小伙伴可以参考下。
    2015-08-08
  • 捕获浏览器关闭、刷新事件不同情况下的处理方法

    捕获浏览器关闭、刷新事件不同情况下的处理方法

    在做一些关于会员在线的问题时,往往我们要根据览器是否关闭来判断用户是否下线,然后再从session和application中将此用户移除,下面与大家分享下具体的捕捉方法
    2013-06-06
  • JavaScript本地存储与会话存储的实现介绍

    JavaScript本地存储与会话存储的实现介绍

    本地存储和会话存储是比较常用的方法,你知道两者的区别吗,本文详细的介绍了JavaScript中本地存储(LocalStorage)和会话存储(SessionStorage)的使用,具有一定的参考价值,感兴趣的可以了解一下
    2022-08-08
  • js实现的文字横向无间断滚动

    js实现的文字横向无间断滚动

    要实现这样一个功能,文字在某块区域内横向无间隙滚动。
    2010-12-12
  • JavaScript面向对象之体会[总结]

    JavaScript面向对象之体会[总结]

    看过很多JavaScript书,对JavaScript的面向对象讲的都比较深入,但是并没有做到深入浅出,总结了我做的一些JavaScript程序的经验,以简洁明了的文字使大家明白JavaScript面向对象的实现。
    2008-11-11
  • 原生js实现省市区三级联动代码分享

    原生js实现省市区三级联动代码分享

    这篇文章主要介绍了原生js实现省市区三级联动功能以及代码分享,对此有需要的朋友可以参考学习下。
    2018-02-02
  • 微信小程序自定义scroll-view的实例代码

    微信小程序自定义scroll-view的实例代码

    这篇文章主要给大家介绍了关于微信小程序自定义scroll-view的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • 13 个JavaScript 性能提升技巧分享

    13 个JavaScript 性能提升技巧分享

    13个简单的代码优化方法,可以让你的代码在 Chrome 的 V8 JavaScript 引擎编译/运行你的 JavaScript 代码更加快速
    2012-07-07
  • JS highcharts实现动态曲线代码示例

    JS highcharts实现动态曲线代码示例

    这篇文章主要介绍了JS highcharts实现动态曲线代码示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10
  • javascript cookie的简单应用

    javascript cookie的简单应用

    这篇文章主要介绍了javascript cookie的简单应用,我们先介绍一下cookie的使用,以及我后面简单封装一个cookie操作的单例,便于实现cookie的设置,获取,删除的方法,感兴趣的小伙伴们可以参考一下
    2016-02-02

最新评论