vue倒計(jì)時(shí)刷新頁面不會(huì)從頭開始的解決方法
開啟倒計(jì)時(shí),直接保存到vuex中,且存儲(chǔ)到本地持久化
// state.jsconst runTime = localStorage.getItem(’time’);paymentRunTime:runTime
// mutations.jsTimeReduction(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) }
知識(shí)點(diǎn)擴(kuò)展:
倒計(jì)時(shí)實(shí)例代碼:
<template> <div class='captcha-row'> <input placeholder='輸入驗(yàn)證碼' auto-focus /> <div v-if='showtime===null' @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ì)從頭開始的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 解決Python 進(jìn)程池Pool中一些坑2. Python如何讀寫CSV文件3. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究4. 三個(gè)不常見的 HTML5 實(shí)用新特性簡介5. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁6. ajax請(qǐng)求添加自定義header參數(shù)代碼7. php測(cè)試程序運(yùn)行速度和頁面執(zhí)行速度的代碼8. Python獲取抖音關(guān)注列表封號(hào)賬號(hào)的實(shí)現(xiàn)代碼9. python利用os模塊編寫文件復(fù)制功能——copy()函數(shù)用法10. Python使用jupyter notebook查看ipynb文件過程解析
