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'>確認(rèn)</button> </view> </view> </template>
具體思路:
三目運算,判斷codeTime的值,當(dāng)為0的時候顯示文字“獲取驗證碼”,大于0的時候顯示驗證碼的倒計時。codeTime默認(rèn)為0.
這里有個問題就是,怎么阻止用戶在倒計時還沒結(jié)束的時候一直點擊,影響倒計時。
解決辦法是寫個判斷,當(dāng)codeTime大于60的時候,彈窗提示用戶不能重復(fù)獲取驗證碼。當(dāng)?shù)褂嫊r運行完了之后要清除倒計時。
代碼:
<script> export default { data() { return { codeTime:0, } }, methods: { getCheckNum(){ if(this.codeTime>0){ uni.showToast({ title: '不能重復(fù)獲取', 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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
實例解析js中try、catch、finally的執(zhí)行規(guī)則
本文主要通過實例解析來更好的了解js中try、catch、finally的執(zhí)行規(guī)則,具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02JavaScript中undefined和null的區(qū)別
這篇文章主要介紹了 JavaScript中undefined和null的區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-05-05用正則表達(dá)式 動態(tài)創(chuàng)建/增加css style script 兼容IE firefox
動態(tài)創(chuàng)建/增加css style script 用正則表達(dá)式 兼容IE firefox2009-03-03