Vue3實(shí)現(xiàn)獲取驗(yàn)證碼按鈕倒計(jì)時(shí)效果
Vue3實(shí)現(xiàn)獲取驗(yàn)證碼按鈕倒計(jì)時(shí)效果
效果描述:用戶點(diǎn)擊獲取驗(yàn)證碼按鈕,發(fā)送請(qǐng)求給后端,按鈕失效,并且開始倒計(jì)時(shí)60秒;在此期間,用戶無法再次點(diǎn)擊按鈕,即使用戶刷新頁面,倒計(jì)時(shí)依然存在,直到倒計(jì)時(shí)完畢,按鈕恢復(fù)。效果圖如下:

實(shí)現(xiàn)思路和具體實(shí)現(xiàn)
實(shí)現(xiàn)思路:
- 通過響應(yīng)式變量來,設(shè)置倒計(jì)時(shí)默認(rèn)數(shù)據(jù)(按鈕名稱,秒數(shù),按鈕狀態(tài))
- 倒計(jì)時(shí)函數(shù),來改變響應(yīng)式變量里的數(shù)據(jù),使用周期函數(shù)(window.setInterval),來循環(huán)執(zhí)行。
- 使用watch函數(shù)來監(jiān)控響應(yīng)式變量里的數(shù)據(jù),是否改變,改變就將數(shù)據(jù)保存到LocalStorage中。
- 使用生命周期函數(shù)onMounted來獲取LocalStorage里面的數(shù)據(jù),防止用戶刷新。
- 最后,將數(shù)據(jù)和函數(shù),綁定到按鈕上。

具體實(shí)現(xiàn):
注意:為了簡(jiǎn)潔明了,代碼里只有一個(gè)輸入框、按鈕和最重要的邏輯,沒有樣式。用了 element-plus框架,可自行修改。
<template>
<div>
<el-input v-model="input" style="width: 125px;margin-right: 10px;" placeholder="請(qǐng)輸入驗(yàn)證碼" />
<el-button type="primary" :disabled="countdownInfo.isDisabled" @click="countdown() " style="width:105px;">{{ countdownInfo.buttonName }}</el-button>
</div>
</template>
<script setup>
import { ElMessage } from 'element-plus';
import { ref,watch,onMounted } from 'vue';
//倒計(jì)時(shí)信息
const countdownInfo = ref({
second: 60, //倒計(jì)時(shí)秒
buttonName: "獲取驗(yàn)證碼", //按鈕名稱
isDisabled: false //按鈕是否有效,默認(rèn)有效
})
//生命周期函數(shù)
onMounted(() => {
//獲取localStorage里保存的,倒計(jì)時(shí)字符串信息,并解析為JSON對(duì)象
var localCountdownInfo = JSON.parse(localStorage.getItem("countdownInfo"));
//判斷獲取的信息是否為空,為空,說明里面沒有保存數(shù)據(jù),就不賦值到countdownInfo中
if (localCountdownInfo) {
countdownInfo.value =localCountdownInfo;
//不為空時(shí),判斷倒計(jì)時(shí)秒數(shù),是否為60,不是就直接調(diào)用倒計(jì)時(shí)函數(shù)(說明沒有倒計(jì)時(shí)完),執(zhí)行倒計(jì)時(shí)。
if (countdownInfo.value.second !== 60) {
countdown() //調(diào)用倒計(jì)時(shí)函數(shù)
}
}
})
// 定義倒計(jì)時(shí)函數(shù)
const countdown=()=> {
countdownInfo.value.isDisabled = true; //按鈕無效
//開始倒計(jì)時(shí)
let interval = window.setInterval(function () {
countdownInfo.value.buttonName = countdownInfo.value.second + "秒后重新獲取"; //重新設(shè)置按鈕名稱
countdownInfo.value.second = countdownInfo.value.second - 1; //倒計(jì)時(shí)減
//判斷是否減到了0,為0就恢復(fù)默認(rèn)信息,并停止倒計(jì)時(shí)
if (countdownInfo.value.second <=0) {
countdownInfo.value.buttonName = "獲取驗(yàn)證碼";
countdownInfo.value.second = 60;
countdownInfo.value.isDisabled = false;
window.clearInterval(interval);
}
}, 1000); //一秒執(zhí)行一次
}
//監(jiān)聽對(duì)象,數(shù)據(jù)是否發(fā)生改變,改變就進(jìn)行重新保存
watch(countdownInfo, (newValue) => {
var JSONSTR = JSON.stringify(newValue); //將JSON轉(zhuǎn)為字符串
localStorage.setItem("countdownInfo", JSONSTR) //將其保存到localStorage中
}, {
deep: true //深度監(jiān)聽
})
</script>
到此這篇關(guān)于Vue3實(shí)現(xiàn)獲取驗(yàn)證碼按鈕倒計(jì)時(shí)效果的文章就介紹到這了,更多相關(guān)Vue3驗(yàn)證碼按鈕倒計(jì)時(shí)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 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)驗(yàn)證碼倒計(jì)時(shí)功能(刷新保持狀態(tài))
- 使用Vue3實(shí)現(xiàn)倒計(jì)時(shí)器及倒計(jì)時(shí)任務(wù)的完整代碼
相關(guān)文章
Vue3之Mixin的使用方式(全局,局部,setup內(nèi)部使用)
這篇文章主要介紹了Vue3之Mixin的使用方式(全局,局部,setup內(nèi)部使用),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue 實(shí)現(xiàn)輸入框新增搜索歷史記錄功能
這篇文章主要介紹了Vue 輸入框新增搜索歷史記錄功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10

