Vue3?實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能
前言
倒計(jì)時(shí)的運(yùn)用場(chǎng)景:獲取手機(jī)驗(yàn)證碼倒計(jì)時(shí)、獲取郵箱驗(yàn)證碼倒計(jì)時(shí)等場(chǎng)景,廢話不多說(shuō),開始吧。
之前給大家介紹過(guò)Vue3 實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能(刷新保持狀態(tài)),需要的朋友點(diǎn)擊查看。
實(shí)現(xiàn)效果
實(shí)現(xiàn)代碼 html(重要部分)
<template> <el-button v-if="!sms.disabled" color="#f38301" type="default" @click="send"><span style="color:#eed2b0;">發(fā)送驗(yàn)證碼</span></el-button> <el-button v-else type="default" color="#f38301" disabled><span style="color:#eed2b0;">{{ sms.count }} 秒后重新發(fā)送</span></el-button> </template>
js(重要部分)
<script lang="ts" setup> // 驗(yàn)證碼計(jì)時(shí)器 const sms = reactive({ disabled: false, total: 60, count: 0 }) // 計(jì)時(shí)器處理器 const timerHandler = () => { sms.count = sms.total sms.disabled = true let timer = setInterval(() => { if (sms.count > 1 && sms.count <= sms.total) { sms.count-- } else { sms.disabled = false clearInterval(timer) } }, 1000) } // 推送 const send = () => { if (formLabelAlign.username === '') { ElMessage.error("電子郵件為空") } else { // 計(jì)時(shí)器處理器 timerHandler() // 加載ui const loadingInstance1 = ElLoading.service({fullscreen: true}) // api 請(qǐng)求 return emailRegisterApi(formLabelAlign.username).then(res => { // 關(guān)閉加載 loadingInstance1.close() }).catch(() => { // 關(guān)閉加載 loadingInstance1.close() }) } </script>
完整代碼
<template> <div class="email-main"> <div class="email-content"> <div class="email-title"> <h3>注冊(cè)報(bào)名</h3> </div> <div class="email-step"> <el-steps :active="active" finish-status="success" align-center="true"> <el-step title="郵箱注冊(cè)" /> <el-step title="填寫報(bào)名信息" /> <el-step title="完成" /> </el-steps> </div> <div class="email-table"> <el-form ref="formRef" label-position="right" :model="formLabelAlign" style="width: 460px" label-width="100px" size="default" > <el-form-item prop="username" label="郵箱賬號(hào)"> <el-input v-model="formLabelAlign.username" /> </el-form-item> <el-form-item prop="region" label="設(shè)置密碼"> <el-input v-model="formLabelAlign.password" /> </el-form-item> <el-form-item prop="code" label="驗(yàn)證碼"> <el-input v-model="formLabelAlign.code" /> </el-form-item> <el-form-item style="margin-left:50px;"> <el-button color="#f38301" @click="submitForm"> <span style="color:#eed2b0;">下一步</span> </el-button> <el-button color="#f38301" @click="resetForm(formRef)"> <span style="color: #eed2b0;">重置</span></el-button> <el-button v-if="!sms.disabled" color="#f38301" type="default" @click="send"><span style="color:#eed2b0;">發(fā)送驗(yàn)證碼</span></el-button> <el-button v-else type="default" color="#f38301" disabled><span style="color:#eed2b0;">{{ sms.count }} 秒后重新發(fā)送</span></el-button> </el-form-item> </el-form> </div> </div> </div> </template> <script lang="ts" setup> import { ref,reactive } from 'vue' import { FormInstance, ElLoading, ElMessage } from 'element-plus' import { emailRegisterApi } from '@/api/facade/facade' import { useRouter } from 'vue-router' // 路由 const router = useRouter() // 步驟標(biāo)識(shí) const active = ref(0) // 重置 const formRef = ref<FormInstance>() // 注冊(cè)參數(shù) const formLabelAlign = reactive({ username: '', password: '', code: '', }) // 驗(yàn)證碼計(jì)時(shí)器 const sms = reactive({ disabled: false, total: 60, count: 0 }) // 計(jì)時(shí)器處理器 const timerHandler = () => { sms.count = sms.total sms.disabled = true let timer = setInterval(() => { if (sms.count > 1 && sms.count <= sms.total) { sms.count-- } else { sms.disabled = false clearInterval(timer) } }, 1000) } const resetForm = (formEl: FormInstance | undefined) => { if (!formEl) return formEl.resetFields() } // 下一步 const submitForm = () => { const loadingInstance1 = ElLoading.service({fullscreen: true}) if (formLabelAlign.username != '' && formLabelAlign.password != '' && formLabelAlign.code != '') { // 跳轉(zhuǎn)到報(bào)名頁(yè)面 router.push({ path: '/sign_up_info', query: {username: formLabelAlign.username, password: formLabelAlign.password, code: formLabelAlign.code} }) } else { ElMessage.warning("請(qǐng)完善全部信息") } loadingInstance1.close() } const send = () => { console.log('codeDisabled:',formLabelAlign.username) if (formLabelAlign.username === '') { ElMessage.error("電子郵件為空") } else { timerHandler() const loadingInstance1 = ElLoading.service({fullscreen: true}) return emailRegisterApi(formLabelAlign.username).then(res => { loadingInstance1.close() }).catch(() => { ElMessage.error("Email數(shù)據(jù)異常") loadingInstance1.close() }) } } </script> <style scoped> .email-main { height: 100%; width: 100%; position: relative; } .email-content { width: 1200px; height: 800px; border: 1px; position: relative; left: 400px; background: #f1f3f5; } .email-title { top: 30px; position: relative; left: 570px; margin-top: 100px; } .email-step { left: 100px; top: 0px; position: absolute; margin-top: 100px; width: 1000px; } .email-table { left: 350px; top: 200px; position: absolute; } :deep(.el-step__icon.is-text){ border-radius: 50%; border: 2px solid #f38301; background: #f38301; color: #f1f3f5; } </style>
PS:Vue3 - 驗(yàn)證碼按鈕倒計(jì)時(shí)實(shí)現(xiàn)
使用 primevue 作為 UI 框架,沒有按鈕加載(失效)倒計(jì)時(shí)的配置項(xiàng)。需要自己實(shí)現(xiàn)。
setup() { const btn_vrft = reactive({ label: '獲取驗(yàn)證碼', loading: false }); function countDownTimer(count) { return new Promise(resolve => { console.log("Start") if(count>0){ setTimeout(() => { count-- countDownTimer(count) }, 1000) btn_vrft.label = count.toString() } else { btn_vrft.loading=false btn_vrft.label='獲取驗(yàn)證碼' } resolve(count) console.log(count) console.log("End") }) }; async function handleVerification(){ btn_vrft.loading=true await countDownTimer(60) }; return { btn_vrft, handleVerification } }
按鈕組件:
<Button @click="handleVerification" :label="btn_vrft.label" :loading="btn_vrft.loading" />
到此這篇關(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實(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í)功能(刷新保持狀態(tài))
- 使用Vue3實(shí)現(xiàn)倒計(jì)時(shí)器及倒計(jì)時(shí)任務(wù)的完整代碼
相關(guān)文章
vue watch普通監(jiān)聽和深度監(jiān)聽實(shí)例詳解(數(shù)組和對(duì)象)
這篇文章主要介紹了vue watch普通監(jiān)聽和深度監(jiān)聽(數(shù)組和對(duì)象),文中單獨(dú)通過(guò)代碼給大家介紹了vue watch 深度監(jiān)聽的方法,感興趣的朋友一起看看吧2018-08-08Vue.js系列之項(xiàng)目結(jié)構(gòu)說(shuō)明(2)
這篇文章主要介紹了Vue.js系列之項(xiàng)目結(jié)構(gòu)說(shuō)明(2)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01vue-element的select下拉框賦值實(shí)例
這篇文章主要介紹了vue-element的select下拉框賦值實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03vue 表單之通過(guò)v-model綁定單選按鈕radio
這篇文章主要介紹了vue 表單之v-model綁定單選按鈕radio的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05在vue-cli項(xiàng)目中使用bootstrap的方法示例
本篇文章主要介紹了在vue-cli項(xiàng)目中使用bootstrap的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04vue-pdf實(shí)現(xiàn)文件在線預(yù)覽
這篇文章主要為大家詳細(xì)介紹了vue-pdf實(shí)現(xiàn)文件在線預(yù)覽,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Vue中的請(qǐng)求攔截器和響應(yīng)攔截器用法及說(shuō)明
這篇文章主要介紹了Vue中的請(qǐng)求攔截器和響應(yīng)攔截器用法及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05