微信小程序?qū)崿F(xiàn)發(fā)送短信驗(yàn)證碼倒計(jì)時(shí)
本文實(shí)例為大家分享了微信小程序發(fā)送短信驗(yàn)證碼倒計(jì)時(shí)的具體代碼,供大家參考,具體內(nèi)容如下
效果圖

WXML文件
<view class="container">
<view class="userinfo">
<image class="userinfo-avatar" src="../../images/timg.jpg" mode="cover"></image>
<text class="userinfo-nickname">什么</text>
</view>
<view class="wrap">
<view class="tel">
<input type="number" bindinput="bindTelInput" maxlength="11"
placeholder="請(qǐng)輸入手機(jī)號(hào)"
placeholder-style="color:#C3C6C4;"/>
</view>
<view class="ver-code">
<view class="code">
<input type="number" bindinput="bindCodeInput" maxlength="6"
placeholder="請(qǐng)輸入驗(yàn)證碼"
placeholder-style="color:#C3C6C4;"/>
</view>
<view class="getCode" bindtap="getCode" wx:if="{{countDownNum == 60 || countDownNum == -1}}">
<button type="primary" plain="true">獲取驗(yàn)證碼</button>
</view>
<view class="getCode" wx:else>
<button type="primary" plain="true">{{countDownNum}}s后重新獲取</button>
</view>
</view>
</view>
<view class="btn-login" bindtap="login">登錄</view>
</view>
JS文件
//獲取應(yīng)用實(shí)例
const app = getApp()
Page({
/**
* 頁面的初始數(shù)據(jù)
*/
data: {
phone: null, // 手機(jī)號(hào)
code: null, // 手機(jī)驗(yàn)證碼
countDownNum: 60, // 倒計(jì)時(shí)初始值
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function (options) {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面顯示
*/
onShow: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成
*/
onReady: function () {
},
/**
* 用戶點(diǎn)擊右上角分享
*/
onShareAppMessage: function () {
},
// 輸入手機(jī)號(hào)
bindTelInput: function (e) {
this.setData({
phone: e.detail.value
})
},
// 輸入驗(yàn)證碼
bindCodeInput: function (e) {
this.setData({
code: e.detail.value
})
},
// 發(fā)送手機(jī)驗(yàn)證碼
getCode: function () {
if (!!this.data.phone) {
if (!!(/^1[34578]\d{9}$/.test(this.data.phone))) {
wx.showToast({
title: "發(fā)送成功",
icon: "none",
duration: 1500
})
this.countDown()
} else {
wx.showToast({
title: "請(qǐng)輸入正確的手機(jī)號(hào)",
icon: "none",
duration: 1500
})
}
} else {
wx.showToast({
title: "請(qǐng)輸入手機(jī)號(hào)",
icon: "none",
duration: 1500
})
}
},
/**
* 驗(yàn)證碼倒計(jì)時(shí)
*/
countDown: function () {
var _this = this
var countDownNum = _this.data.countDownNum // 獲取倒計(jì)時(shí)初始值
var timer = setInterval(function () {
countDownNum -= 1
_this.setData({
countDownNum: countDownNum
})
if (countDownNum <= -1) {
clearInterval(timer)
// 取消置頂?shù)膕etInterval函數(shù)將要執(zhí)行的代碼
_this.setData({
countDownNum: 60
})
}
}, 1000)
},
// 手機(jī)驗(yàn)證碼登錄
login: function () {
if (this.data.phone) {
if (!!(/^1[34578]\d{9}$/.test(this.data.phone))) {
if (this.data.code) {
wx.showToast({
title: "登錄成功",
icon: "none",
duration: 1500
})
} else {
wx.showToast({
title: "請(qǐng)輸入驗(yàn)證碼",
icon: "none",
duration: 1500
})
}
} else {
wx.showToast({
title: "請(qǐng)輸入正確的手機(jī)號(hào)",
icon: "none",
duration: 1500
})
}
} else {
wx.showToast({
title: "請(qǐng)輸入手機(jī)號(hào)",
icon: "none",
duration: 1500
})
}
}
})
WXSS文件
.userinfo {
height: 240rpx;
margin: 40rpx auto 0;
display: flex;
flex-direction: column;
align-items: center;
}
.userinfo .userinfo-avatar {
width: 140rpx;
height: 140rpx;
margin: 20rpx;
border-radius: 50%;
border: 1rpx solid #dad5d5;
}
.userinfo .userinfo-nickname {
color: #aaa;
}
.wrap {
width: 630rpx;
font-size: 32rpx;
margin: 80rpx auto 120rpx;
}
.wrap .tel {
width: 100%;
height: 68rpx;
border-bottom: 1rpx solid #DDE3EC;
margin-bottom: 60rpx;
display: flex;
flex-direction: column;
justify-content: flex-start;
}
.wrap .ver-code {
width: 100%;
height: 68rpx;
border-bottom: 1rpx solid #DDE3EC;
display: flex;
justify-content: space-between;
}
.wrap .ver-code .code {
}
.wrap .ver-code .getCode {
min-width: 190rpx;
height: 40rpx;
}
.wrap .ver-code .getCode button {
width: 100%;
height: 100%;
font-size: 28rpx;
font-weight: normal;
line-height: 40rpx;
background: #fff;
color: #ffaa7f;
border: none;
padding: 0;
margin: 0;
}
.btn-login {
width: 588rpx;
height: 88rpx;
background: #ffaa7f;
border-radius: 10rpx;
text-align: center;
line-height: 88rpx;
font-size: 36rpx;
font-weight: 500;
color: #fff;
margin: 0 auto;
}
.clickClass {
background: #ea986c;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Ajax使用原生態(tài)JS驗(yàn)證用戶名是否存在
這篇文章主要為大家詳細(xì)介紹了Ajax使用原生態(tài)JS驗(yàn)證用戶名是否存在的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Layui數(shù)據(jù)表格判斷編輯輸入的值,是否為我需要的類型詳解
今天小編就為大家分享一篇Layui數(shù)據(jù)表格判斷編輯輸入的值,是否為我需要的類型詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10
使用do...while的方法輸入一個(gè)月中所有的周日(實(shí)例代碼)
下面小編就為大家?guī)硪黄褂胐o...while的方法輸入一個(gè)月中所有的周日(實(shí)例代碼)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07
JavaScript暫時(shí)性死區(qū)以及函數(shù)作用域
這篇文章主要為大家介紹了JavaScript暫時(shí)性死區(qū)以及函數(shù)作用域示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
JavaScript類型檢測(cè)的方法實(shí)例教程
這篇文章主要給大家介紹了關(guān)于JavaScript類型檢測(cè)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
JS實(shí)現(xiàn)簡(jiǎn)易圖片輪播效果的方法
這篇文章主要介紹了JS實(shí)現(xiàn)簡(jiǎn)易圖片輪播效果的方法,實(shí)例分析了javascript操作圖片實(shí)現(xiàn)輪播特效的技巧,需要的朋友可以參考下2015-03-03
BootStrap Validator 版本差異問題導(dǎo)致的submitHandler失效問題的解決方法
這篇文章主要介紹了BootStrap Validator 版本差異問題導(dǎo)致的submitHandler失效問題的解決方法,下面通過本文給大家詳細(xì)說明一下,需要的朋友可以參考下2016-12-12
javascript 動(dòng)態(tài)樣式添加的簡(jiǎn)單實(shí)現(xiàn)
下面小編就為大家?guī)硪黄猨avascript 動(dòng)態(tài)樣式添加的簡(jiǎn)單實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10

