Vue3?實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能(刷新保持狀態(tài))
前言
倒計(jì)時(shí)的運(yùn)用場(chǎng)景是需要經(jīng)常用到的,但是根據(jù)業(yè)務(wù)的不同,好比手機(jī)驗(yàn)證碼或者是郵箱驗(yàn)證碼之類(lèi)的,即使用戶(hù)跳轉(zhuǎn)到其它頁(yè)面或者刷新,再次回到登錄也,驗(yàn)證碼的倒計(jì)時(shí)也得保持狀態(tài),大家參考邏輯即可,每個(gè)人的項(xiàng)目不同,這里提供大概的實(shí)現(xiàn)代碼。
代碼實(shí)現(xiàn)
主要邏輯
const state = reactive({ labelPosition: 'top', formData: { phone: '', code: '', }, // 倒計(jì)時(shí) countDownTime: 60, timer: null, countDownIng: false, }) const countDown = () => { let startTime = localStorage.getItem('startTimeLogin'); let nowTime = new Date().getTime(); if (startTime) { let surplus = 60 - parseInt((nowTime - startTime) / 1000, 10); state.countDownTime = surplus <= 0 ? 0 : surplus; } else { state.countDownTime = 60; localStorage.setItem('startTimeLogin', nowTime); } state.timer = setInterval(() => { state.countDownTime--; state.getCodeDisabled = true; state.countDownIng = true; if (state.countDownTime <= 0) { localStorage.removeItem('startTimeLogin'); clearInterval(state.timer); state.countDownTime = 60; state.countDownIng = false; } }, 1000) }
onMounted 方法
onMounted(() => { let sendEndTime = localStorage.getItem('startTimeLogin'); if (sendEndTime) { state.countDownIng = true; countDown() } })
完整代碼
<template> <main class="content"> <div class="mi-card login-card"> <div class="reg-form"> <el-form :label-position="labelPosition" label-width="auto" :model="formData"> <el-form-item label="手機(jī)號(hào)"> <el-input v-model="formData.phone" placeholder="手機(jī)號(hào)"> <template #append> <div class="get-code"> <span v-if="!countDownIng" @click="countDown">獲取驗(yàn)證碼</span> <span v-if="countDownIng">倒計(jì)時(shí){{ countDownTime }}s</span> </div> </template> </el-input> </el-form-item> <el-form-item label="驗(yàn)證碼"> <el-input v-model="formData.code" placeholder="驗(yàn)證碼"/> </el-form-item> <el-form-item> <span class="sub-btn to-login" style="width: 100%">注冊(cè)</span> </el-form-item> </el-form> </main> </template> <script> import {defineComponent, reactive, toRefs, onMounted} from 'vue' export default defineComponent({ name: "LoginPage", setup() { const state = reactive({ labelPosition: 'top', formData: { phone: '', code: '', }, // 倒計(jì)時(shí) countDownTime: 60, timer: null, countDownIng: false, }) /** * 作者:故藍(lán)尋 * 時(shí)間:2022/08/05 17:13:37 * 功能:獲取驗(yàn)證碼 事件 */ const countDown = () => { let startTime = localStorage.getItem('startTimeLogin'); let nowTime = new Date().getTime(); if (startTime) { let surplus = 60 - parseInt((nowTime - startTime) / 1000, 10); state.countDownTime = surplus <= 0 ? 0 : surplus; } else { state.countDownTime = 60; localStorage.setItem('startTimeLogin', nowTime); } state.timer = setInterval(() => { state.countDownTime--; state.getCodeDisabled = true; state.countDownIng = true; if (state.countDownTime <= 0) { localStorage.removeItem('startTimeLogin'); clearInterval(state.timer); state.countDownTime = 60; state.countDownIng = false; } }, 1000) } onMounted(() => { let sendEndTime = localStorage.getItem('startTimeLogin'); if (sendEndTime) { state.countDownIng = true; countDown() } }) return { ...toRefs(state), countDown } } }) </script> <style scoped lang="scss"> </style>
小結(jié)
- 大致邏輯是這樣,大家根據(jù)具體的需要來(lái)完善,vue2也適用
- 這是vue3,但是使用的js語(yǔ)法,使用的是ts語(yǔ)法的,也是一樣的邏輯
到此這篇關(guān)于Vue3 驗(yàn)證碼倒計(jì)時(shí)實(shí)現(xiàn)(刷新保持狀態(tài))的文章就介紹到這了,更多相關(guān)Vue3 驗(yàn)證碼倒計(jì)時(shí)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue3實(shí)現(xiàn)獲取驗(yàn)證碼按鈕倒計(jì)時(shí)效果
- Vue3動(dòng)態(tài)倒計(jì)時(shí)的代碼實(shí)現(xiàn)
- Vue3+Hooks實(shí)現(xiàn)4位隨機(jī)數(shù)和60秒倒計(jì)時(shí)的示例代碼
- vue3實(shí)現(xiàn)封裝時(shí)間計(jì)算-日期倒計(jì)時(shí)組件-還有XX天&第XX天
- 基于Vue3創(chuàng)建一個(gè)簡(jiǎn)單的倒計(jì)時(shí)組件
- vue3發(fā)送驗(yàn)證碼倒計(jì)時(shí)功能的實(shí)現(xiàn)(防止連點(diǎn)、封裝復(fù)用)
- Vue3?實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能
- 使用Vue3實(shí)現(xiàn)倒計(jì)時(shí)器及倒計(jì)時(shí)任務(wù)的完整代碼
相關(guān)文章
element組件中自定義組件的樣式不生效問(wèn)題(vue scoped scss無(wú)效)
這篇文章主要介紹了解決element組件中自定義組件的樣式不生效問(wèn)題(vue scoped scss無(wú)效),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09nginx+vite項(xiàng)目打包以及部署的詳細(xì)過(guò)程
我們使用nginx部署Vue項(xiàng)目,實(shí)質(zhì)上就是將Vue項(xiàng)目打包后的內(nèi)容同步到nginx指向的文件夾,下面這篇文章主要給大家介紹了關(guān)于nginx+vite項(xiàng)目打包以及部署的相關(guān)資料,需要的朋友可以參考下2023-01-01vue實(shí)現(xiàn)兄弟組件之間跳轉(zhuǎn)指定tab標(biāo)簽頁(yè)
這篇文章主要介紹了vue實(shí)現(xiàn)兄弟組件之間跳轉(zhuǎn)指定tab標(biāo)簽頁(yè),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05vue開(kāi)發(fā)中如何在js文件里使用pinia和組件同步
在JavaScript文件中封裝涉及到使用Pinia的方法時(shí),發(fā)現(xiàn)Pinia和組件內(nèi)容并不同步,二者的狀態(tài)是獨(dú)立的,解決辦法是,在新建對(duì)象的時(shí)候,將Pinia作為參數(shù)傳入,本文給大家介紹vue開(kāi)發(fā)中如何在js文件里使用pinia和組件同步,感興趣的朋友一起看看吧2024-10-10淺談vue中$event理解和框架中在包含默認(rèn)值外傳參
這篇文章主要介紹了淺談vue中$event理解和框架中在包含默認(rèn)值外傳參,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08vue3限制table表格選項(xiàng)個(gè)數(shù)的解決方法
這篇文章主要為大家詳細(xì)介紹了vue3限制table表格選項(xiàng)個(gè)數(shù)的解決方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04vue結(jié)合Axios+v-for列表循環(huán)實(shí)現(xiàn)網(wǎng)易健康頁(yè)面(實(shí)例代碼)
這篇文章主要介紹了vue結(jié)合Axios+v-for列表循環(huán)實(shí)現(xiàn)網(wǎng)易健康頁(yè)面,在項(xiàng)目下安裝axios,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03