今天就跟大家聊聊有關使用uni-app如何實現獲取驗證碼倒計時功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
頁面部分是一個三目運算,codeTime是倒計時的時間。
<template>
<view>
<view class="three">
<view class="get" @tap="getCheckNum()">
<text>{{!codeTime?'獲取驗證碼':codeTime+'s'}}</text>
</view>
<view class="all">
<view class="left">驗證碼</view>
<input v-model="mydata.checkNum" placeholder="請輸入驗證碼"/>
</view>
<button class="btn" @tap='sure'>確認</button>
</view>
</view>
</template>具體思路:
三目運算,判斷codeTime的值,當為0的時候顯示文字“獲取驗證碼”,大于0的時候顯示驗證碼的倒計時。codeTime默認為0.
這里有個問題就是,怎么阻止用戶在倒計時還沒結束的時候一直點擊,影響倒計時。
解決辦法是寫個判斷,當codeTime大于60的時候,彈窗提示用戶不能重復獲取驗證碼。當倒計時運行完了之后要清除倒計時。
代碼:
<script>
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<1){
clearInterval(timer);
this.codeTime = 0
}
},1000)
}
}
}
}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;
}看完上述內容,你們對使用uni-app如何實現獲取驗證碼倒計時功能有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。