微信小程序实现获取验证码60S倒计时(学习微信小程序第n天)!
程序员文章站
2024-03-13 23:34:52
...
1.wxml页面
<text class='key_code' wx:if="{{is_showCode}}" bindtap='getCode'>获取验证码</text>
<text wx:else class='key_code'>{{minutes}}s后重新发送</text>
</view>
<view class='login_tr'>
<input type="number" placeholder="请输入验证码" maxlength="6" class='ipt'
placeholder-class='placeholders' name="userPassword" bindinput='get_code'/>
</view>
<view>
<i-button type="info" bindtap='loginTab'>登录</i-button>
</view>
2.login.js页面
//初始化数据
data: {
is_showCode: true, //判断有没有点击获取验证码
minutes: "60", //获取验证码的毫秒数
},
// 获取验证码倒计时
getCode: function (event) {
var that = this
var count = 59;
that.setData({
is_showCode: false //显示倒计时的span
})
var timer = setInterval(function () {
if (count >= 0) {
that.setData({
minutes: count
})
} else {
clearInterval(timer);
that.setData({ is_showCode: true })
}
count--
}, 1000)
结束…