vue倒計時刷新頁面不會從頭開始的解決方法
開啟倒計時,直接保存到vuex中,且存儲到本地持久化
// 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) }
知識點擴(kuò)展:
倒計時實例代碼:
<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 {
// 計時器,注意需要進(jìn)行銷毀
timeCounter: null,
// null 則顯示按鈕 秒數(shù)則顯示讀秒
showtime: null
}
},
methods: {
// 倒計時顯示處理
countDownText(s) {
this.showtime = `${s}s后重新獲取`
},
// 倒計時 60秒 不需要很精準(zhǔn)
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>
以上就是vue倒計時刷新頁面不會從頭開始的解決方法的詳細(xì)內(nèi)容,更多關(guān)于vue倒計時刷新頁面不會從頭開始的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解從零搭建 vue2 vue-router2 webpack3 工程
本篇文章主要介紹了詳解從零搭建 vue2 vue-router2 webpack3 工程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
Vue實施重新發(fā)布和軟件熱更新的經(jīng)驗分享
在Web應(yīng)用的開發(fā)周期中,版本管理和軟件熱更新是一個不可或缺的話題,隨著Vue.js框架的流行,越來越多的應(yīng)用程序選擇使用Vue來構(gòu)建前端,本文將探討在Vue應(yīng)用中實施重新發(fā)布和熱更新的最佳實踐,需要的朋友可以參考下2024-09-09
vue關(guān)于重置表單數(shù)據(jù)出現(xiàn)undefined的解決
這篇文章主要介紹了vue關(guān)于重置表單數(shù)據(jù)出現(xiàn)undefined的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
vue 實現(xiàn)websocket發(fā)送消息并實時接收消息
這篇文章主要介紹了vue 實現(xiàn)websocket發(fā)送消息并實時接收消息,項目結(jié)合vue腳手架和websocket來搭建,本文給大家分享實例代碼,需要的朋友可以參考下2019-12-12
手動實現(xiàn)vue2.0的雙向數(shù)據(jù)綁定原理詳解
這篇文章主要給大家介紹了關(guān)于手動實現(xiàn)vue2.0的雙向數(shù)據(jù)綁定原理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
Vue2 Element Schema Form 配置式生成表單的實現(xiàn)
本文主要介紹了Vue2 Element Schema Form 配置式生成表單的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05

