vue獲取驗證碼倒計時組件
更新時間:2019年08月26日 11:06:09 作者:曹天驕
這篇文章主要為大家詳細介紹了vue獲取驗證碼倒計時組件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue獲取驗證碼倒計時組件,供大家參考,具體內(nèi)容如下
之前寫過一個計時函數(shù),有計算誤差,但是驗證碼的60秒倒計時可以忽略這一點點誤差。直接上代碼。

<template>
<div class="captcha-row">
<input class="captcha-input" placeholder="輸入驗證碼" auto-focus />
<div v-if="showtime===null" class="captcha-button" @click="send">
獲取驗證碼
</div>
<div v-else class="captcha-button">
{{showtime}}
</div>
</div>
</template>
<script>
export default {
data() {
return {
// 計時器,注意需要進行銷毀
timeCounter: null,
// null 則顯示按鈕 秒數(shù)則顯示讀秒
showtime: null
}
},
methods: {
// 倒計時顯示處理
countDownText(s) {
this.showtime = `${s}s后重新獲取`
},
// 倒計時 60秒 不需要很精準
countDown(times) {
const self = this;
// 時間間隔 1秒
const interval = 1000;
let count = 0;
self.timeCounter = setTimeout(countDownStart, interval);
function countDownStart() {
if (self.timeCounter == null) {
return false;
}
count++
self.countDownText(times - count + 1);
if (count > times) {
clearTimeout(self.timeCounter)
self.showtime = null;
} else {
self.timeCounter = setTimeout(countDownStart, interval)
}
}
},
send() {
this.countDown(60);
}
},
}
</script>
<style lang="less" scoped>
.captcha-row {
display: flex;
.captcha-button {
background: #048fff;
color: white;
display: flex;
justify-content: center;
align-items: center;
padding: 4rpx 8rpx;
width: 320rpx;
border-radius: 4rpx;
}
}
</style>
更多關(guān)于倒計時的文章請查看專題:《倒計時功能》
更多JavaScript時鐘特效點擊查看:JavaScript時鐘特效專題
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue.js+Echarts開發(fā)圖表放大縮小功能實例
本篇文章主要介紹了vue.js+Echarts開發(fā)圖表放大縮小功能實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
Vue報錯:TypeError:Cannot create property '
這篇文章主要介紹了Vue報錯:TypeError:Cannot create property 'xxx' on string 'xxxx'問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
vue 2 實現(xiàn)自定義組件一到多個v-model雙向數(shù)據(jù)綁定的方法(最新推薦)
有時候我們需要對一個組件綁定自定義 v-model,以更方便地實現(xiàn)雙向數(shù)據(jù),例如自定義表單輸入控件,這篇文章主要介紹了vue 2 實現(xiàn)自定義組件一到多個v-model雙向數(shù)據(jù)綁定的方法,需要的朋友可以參考下2024-07-07

