本文實例為大家分享了uni-app實現獲取驗證碼倒計時的具體代碼,供大家參考,具體內容如下
實現的效果



頁面部分是一個三目運算,codeTime是倒計時的時間。愛掏網 - it200.com
{{!codeTime?'獲取驗證碼':codeTime+'s'}} 驗證碼
具體思路:
三目運算,判斷codeTime的值,當為0的時候顯示文字“獲取驗證碼”,大于0的時候顯示驗證碼的倒計時。愛掏網 - it200.comcodeTime默認為0.
這里有個問題就是,怎么阻止用戶在倒計時還沒結束的時候一直點擊,影響倒計時。愛掏網 - it200.com
解決辦法是寫個判斷,當codeTime大于60的時候,彈窗提示用戶不能重復獲取驗證碼。愛掏網 - it200.com當倒計時運行完了之后要清除倒計時。愛掏網 - it200.com
代碼:
export default {
data() {
return {
codeTime:0,
}
},
methods: {
getCheckNum(){
if(this.codeTime>0){
uni.showToast({
title: '不能重復獲取',
icon:"none"
});
return;
}else{
this.codeTime = 60
let timer = setInterval(()=>{
this.codeTime--;
if(this.codeTime
css樣式:
.all{
margin: 30rpx;
border-bottom: 2rpx solid #EEEEEE;
display: flex;
flex-wrap: nowrap;
}
.left{
margin-bottom: 30rpx;
margin-right: 40rpx;
width: 150rpx;
}
.three{
background-color: white;
width: 92%;
border-radius: 10rpx;
padding: 20rpx 0;
margin: 20rpx auto;
position: relative;
}
.btn{
background-color: orange;
font-size: 28rpx;
width: 160rpx;
height: 70rpx;
line-height: 70rpx;
margin-top: 40rpx;
color: white;
font-weight: 600;
}
.get{
position: absolute;
top: 40rpx;
right: 30rpx;
background-color: orange;
height: 70rpx;
line-height: 70rpx;
color: white;
border-radius: 10rpx;
padding: 0 20rpx;
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。愛掏網 - it200.com
聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。