Vue利用localStorage本地緩存使頁面刷新驗(yàn)證碼不清零功能的實(shí)現(xiàn)
今天我們使用本地緩存localStorage來實(shí)現(xiàn)頁面刷新了,驗(yàn)證碼倒計(jì)時還是和刷新時一樣,而不是清零,其次我們可以使用localStorage去實(shí)現(xiàn)用戶信息緩存,記住密碼等等關(guān)于緩存的功能,在這里就簡單演示一下驗(yàn)證碼功能。
一、功能實(shí)現(xiàn)
話不多說,直接上代碼
<template>
<button @click="getCode()" :disabled="!show">
<span v-show="show">發(fā)送驗(yàn)證碼</span>
<span v-show="!show" class="count">{{count}} s</span>
</button>
</template>
<script>
let TIME_COUNT = 60; // 設(shè)置一個全局的倒計(jì)時的時間
export default {
data() {
return {
show: true,
count: '',
timer: null,
}
},
components: {
marquee
},
created(){
// 進(jìn)入頁面時獲取倒計(jì)時中止的位置,并繼續(xù)計(jì)時
if (localStorage.regtime > 0 && localStorage.regtime <= TIME_COUNT){
TIME_COUNT = localStorage.regtime;
this.count = TIME_COUNT;
this.show = false;
this.timer = setInterval(() => {
if (this.count > 0 && this.count <= TIME_COUNT) {
this.count--
localStorage.regtime = this.count;
} else {
this.show = true;
clearInterval(this.timer);
this.timer = null
}
}, 1000)
}
},
methods: {
getCode () {
// 驗(yàn)證碼倒計(jì)時
if (!this.timer) {
this.count = TIME_COUNT
localStorage.regtime = this.count;
this.show = false
this.timer = setInterval(() => {
if (this.count > 0 && this.count <= TIME_COUNT) {
this.count--
localStorage.regtime = this.count;
} else {
this.show = true
clearInterval(this.timer)
this.timer = null
}
}, 1000)
}
}
}
</script>
二、知識拓展
1.對比cookies,sessionStorage 和 localStorage 三大緩存的主要區(qū)別
1)存儲大小
- cookie數(shù)據(jù)大小不能超過4k。
- sessionStorage和localStorage 雖然也有存儲大小的限制,但比cookie大得多,可以達(dá)到5M或更大。
2)有效時間
- localStorage:存儲持久數(shù)據(jù),瀏覽器關(guān)閉后數(shù)據(jù)不丟失除非主動刪除數(shù)據(jù);
- sessionStorage:數(shù)據(jù)在當(dāng)前瀏覽器窗口關(guān)閉后自動刪除。
- cookie:設(shè)置的cookie過期時間之前一直有效,即使窗口或?yàn)g覽器關(guān)閉,
3)數(shù)據(jù)與服務(wù)器之間的交互方式
- cookie的數(shù)據(jù)會自動的傳遞到服務(wù)器,服務(wù)器端也可以寫cookie到客戶端。
- sessionStorage僅在本地保存,只能在同一標(biāo)簽下共享。
- localStorage僅在本地保存,同一瀏覽器,標(biāo)簽頁全部共享。
4)適合場景使用
- localStorage:適合用于用戶離開不清除的數(shù)據(jù),如記住密碼。
- sessionStorage:適合用于做一些用戶離開時及清除的數(shù)據(jù),如用戶信息。
- cookie:適合用于和服務(wù)器交互的數(shù)據(jù),如用戶發(fā)起請求的唯一憑證。
當(dāng)然只是說誰更適合,存在即合理,別和我杠。
2.localStorage寫法
localStorage.getItem("code")//或localStorage.code或localStorage["code"],獲取code
localStorage.setItem("code","A")//或localStorage.code="A"或localStorage["code"]="A",存儲code
localStorage.removeItem("code")//存儲的持久數(shù)據(jù)不清除是不會丟失的,清除code
localStorage.clear(); //清除本地全部localStorage緩存
總結(jié)
到此這篇關(guān)于Vue利用localStorage本地緩存使頁面刷新驗(yàn)證碼不清零的文章就介紹到這了,更多相關(guān)Vue頁面刷新驗(yàn)證碼不清零內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
原生JS實(shí)現(xiàn)Vue transition fade過渡動畫效果示例
這篇文章主要為大家介紹了原生JS實(shí)現(xiàn)Vue transition fade過渡動畫效果示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
vue項(xiàng)目中請求數(shù)據(jù)特別多導(dǎo)致頁面卡死的解決
這篇文章主要介紹了vue項(xiàng)目中請求數(shù)據(jù)特別多導(dǎo)致頁面卡死的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
在Vue中使用動態(tài)import()語法動態(tài)加載組件
在Vue中,你可以使用動態(tài)import()語法來動態(tài)加載組件,動態(tài)導(dǎo)入允許你在需要時異步加載組件,這樣可以提高應(yīng)用程序的初始加載性能,本文給大家介紹在Vue中使用動態(tài)import()語法動態(tài)加載組件,感興趣的朋友一起看看吧2023-11-11
vue 使用Jade模板寫html,stylus寫css的方法
這篇文章主要介紹了vue 使用Jade模板寫html,stylus寫css的方法,文中還給大家提到了使用jade注意事項(xiàng),需要的朋友可以參考下2018-02-02

