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



頁面部分是一個三目運算,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.
這里有個問題就是,怎么阻止用戶在倒計時還沒結(jié)束的時候一直點擊,影響倒計時。
解決辦法是寫個判斷,當codeTime大于60的時候,彈窗提示用戶不能重復獲取驗證碼。當?shù)褂嫊r運行完了之后要清除倒計時。
代碼:
<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;
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
JavaScript中常用的五種數(shù)字千分位格式化方法
數(shù)字格式化是開發(fā)中經(jīng)常遇到的任務,特別是在需要為數(shù)字添加千分位符或控制小數(shù)位數(shù)時,以下是幾種常用的數(shù)字格式化方法,每種方法有其優(yōu)缺點,適用于不同的需求場景,感興趣的小伙伴跟著小編一起來看看吧2024-12-12
JavaScript實現(xiàn)url地址自動檢測并添加URL鏈接示例代碼
寫一個簡單的聊天系統(tǒng),發(fā)出Htpp的Url實現(xiàn)跳轉(zhuǎn)加上a標簽,下面是具體的實現(xiàn),感興趣的朋友不要錯過2013-11-11
微信小程序開發(fā)之選項卡(窗口底部TabBar)頁面切換
本文主要介紹了微信小程序開發(fā)之選項卡(窗口底部TabBar)頁面切換的相關知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04

