uni-app實(shí)現(xiàn)獲取驗(yàn)證碼倒計(jì)時(shí)功能
本文實(shí)例為大家分享了uni-app實(shí)現(xiàn)獲取驗(yàn)證碼倒計(jì)時(shí)的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)的效果
頁(yè)面部分是一個(gè)三目運(yùn)算,codeTime是倒計(jì)時(shí)的時(shí)間。
<template> <view> <view class="three"> <view class="get" @tap="getCheckNum()"> <text>{{!codeTime?'獲取驗(yàn)證碼':codeTime+'s'}}</text> </view> <view class="all"> <view class="left">驗(yàn)證碼</view> <input v-model="mydata.checkNum" placeholder="請(qǐng)輸入驗(yàn)證碼"/> </view> <button class="btn" @tap='sure'>確認(rèn)</button> </view> </view> </template>
具體思路:
三目運(yùn)算,判斷codeTime的值,當(dāng)為0的時(shí)候顯示文字“獲取驗(yàn)證碼”,大于0的時(shí)候顯示驗(yàn)證碼的倒計(jì)時(shí)。codeTime默認(rèn)為0.
這里有個(gè)問(wèn)題就是,怎么阻止用戶在倒計(jì)時(shí)還沒(méi)結(jié)束的時(shí)候一直點(diǎn)擊,影響倒計(jì)時(shí)。
解決辦法是寫(xiě)個(gè)判斷,當(dāng)codeTime大于60的時(shí)候,彈窗提示用戶不能重復(fù)獲取驗(yàn)證碼。當(dāng)?shù)褂?jì)時(shí)運(yùn)行完了之后要清除倒計(jì)時(shí)。
代碼:
<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; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深入了解JavaScript的邏輯運(yùn)算符(與、或)
本篇文章分享的是 JS 當(dāng)中的邏輯運(yùn)算符與、或,也就是 && 、 || ,沒(méi)錯(cuò),別看這簡(jiǎn)簡(jiǎn)單單的幾個(gè)運(yùn)算符,雖然這是最基礎(chǔ)的知識(shí),但其中隱藏的奧秘卻十分耐人尋味,接下來(lái)本文就為大家一一揭開(kāi)這簡(jiǎn)單的運(yùn)算符背后的奇妙之處。2016-12-12實(shí)例解析js中try、catch、finally的執(zhí)行規(guī)則
本文主要通過(guò)實(shí)例解析來(lái)更好的了解js中try、catch、finally的執(zhí)行規(guī)則,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02JS實(shí)現(xiàn)圖片高斯模糊切換效果的焦點(diǎn)圖實(shí)例
這篇文章主要給大家介紹了利用原生JS如何實(shí)現(xiàn)圖片高斯模糊切換效果的焦點(diǎn)圖,文中給出了完整的實(shí)例代碼,相信對(duì)大家的理解和學(xué)習(xí)具有一定的參考借鑒價(jià)值,下面來(lái)一起看看吧。2017-01-01JavaScript中undefined和null的區(qū)別
這篇文章主要介紹了 JavaScript中undefined和null的區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-05-05用正則表達(dá)式 動(dòng)態(tài)創(chuàng)建/增加css style script 兼容IE firefox
動(dòng)態(tài)創(chuàng)建/增加css style script 用正則表達(dá)式 兼容IE firefox2009-03-03