vue倒計(jì)時(shí)刷新頁面不會(huì)從頭開始的解決方法
開啟倒計(jì)時(shí),直接保存到vuex中,且存儲(chǔ)到本地持久化
// state.js
const runTime = localStorage.getItem('time');
paymentRunTime:runTime
// mutations.js
TimeReduction(state) {
this.timerId = setInterval(() => {
if (state.paymentRunTime === 0) {
state.paymentRunTime = 60;
return clearInterval(this.timerId)
}
state.paymentRunTime -= 1;
localStorage.setItem('time',state.paymentRunTime)
},1000);
},
在需要用到的頁面鉤子函數(shù)調(diào)用方法, created(){ this.$store.commit(TimeReduction) }
知識點(diǎn)擴(kuò)展:
倒計(jì)時(shí)實(shí)例代碼:
<template>
<div class="captcha-row">
<input class="captcha-input" placeholder="輸入驗(yàn)證碼" auto-focus />
<div v-if="showtime===null" class="captcha-button" @click="send">
獲取驗(yàn)證碼
</div>
<div v-else class="captcha-button">
{{showtime}}
</div>
</div>
</template>
<script>
export default {
data() {
return {
// 計(jì)時(shí)器,注意需要進(jìn)行銷毀
timeCounter: null,
// null 則顯示按鈕 秒數(shù)則顯示讀秒
showtime: null
}
},
methods: {
// 倒計(jì)時(shí)顯示處理
countDownText(s) {
this.showtime = `${s}s后重新獲取`
},
// 倒計(jì)時(shí) 60秒 不需要很精準(zhǔn)
countDown(times) {
const self = this;
// 時(shí)間間隔 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>
以上就是vue倒計(jì)時(shí)刷新頁面不會(huì)從頭開始的解決方法的詳細(xì)內(nèi)容,更多關(guān)于vue倒計(jì)時(shí)刷新頁面不會(huì)從頭開始的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue實(shí)現(xiàn)動(dòng)態(tài)圓環(huán)百分比進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)動(dòng)態(tài)圓環(huán)百分比進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
優(yōu)雅的處理vue項(xiàng)目異常實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于如何優(yōu)雅的處理vue項(xiàng)目異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
簡化版的vue-router實(shí)現(xiàn)思路詳解
這篇文章主要介紹了簡化版的vue-router,需要的朋友可以參考下2018-10-10
在vue中使用export?default導(dǎo)出的class類方式
這篇文章主要介紹了在vue中使用export?default導(dǎo)出的class類方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

