vue 防止多次點(diǎn)擊的實(shí)踐
一般點(diǎn)擊事件會(huì)分不同的情況進(jìn)行消息提醒,如果不做處理,短短幾秒彈出很多條提示信息,就會(huì)很煩,比如:
那要怎么控制這個(gè)提示信息只能出現(xiàn)單條呢
再點(diǎn)擊事件的方法最前面加上
定義變量hasRemind來控制是否執(zhí)行點(diǎn)擊事件里的相應(yīng)操作
當(dāng)用戶第一次點(diǎn)擊的時(shí)候,hasRemind = false,此時(shí),進(jìn)入到第二個(gè)if語句,講hasRemind的值改變?yōu)閠rue,并且在3秒后再將hasRemind的值改為false,這是情況下,用戶可以正常進(jìn)入到點(diǎn)擊事件里的所有流程
當(dāng)用戶第二次點(diǎn)擊的時(shí)候,hasRemind=true,此時(shí)直接跳出點(diǎn)擊事件,等待hasRemind的值為false的時(shí)候才能繼續(xù)進(jìn)行該點(diǎn)擊方法里的系列流程
//默認(rèn)可以觸發(fā)登錄的點(diǎn)擊事件 hasRemind:false,
//防止連續(xù)多次點(diǎn)擊 let vm = this; if(this.hasRemind === true) return; if(this.hasRemind === false){ this.hasRemind = true; setTimeout(function(){ vm.hasRemind = false; },3000) }
(這里就是將上述代碼段放在了登錄的點(diǎn)擊事件里,以防止用戶多次點(diǎn)此,出現(xiàn)很多條提示信息的情況)
// "個(gè)人登錄點(diǎn)擊事件" registerBtn() { //防止連續(xù)多次點(diǎn)擊 let vm = this; if(this.hasRemind === true) return; if(this.hasRemind === false){ this.hasRemind = true; setTimeout(function(){ vm.hasRemind = false; },3000) } var qs = Qs; if (this.logintype == 1) { //賬號(hào)密碼登錄 if (this.username == "") { this.$message({ message: '請(qǐng)輸入賬號(hào)', type: 'warning' }); return false; } else if (this.password == "") { this.$message({ message: '請(qǐng)輸入密碼', type: 'warning' }); return false; } else { request_POST('/login', qs.stringify({ identity: this.username, desStr: this.password, loginType: 1, loginRole: 0 })).then((res) => { if (res.data.code == 200) { localStorage.setItem("token", res.data.data["JEECMS-Auth-Token"]); //登陸成功 // window.open(this.apiHost + 'uesr/resume', '_parent') window.open(this.apiHost + 'index/index', '_parent') } else if (res.data.code == 12462) { this.$message({ message: '用戶未注冊(cè)', type: 'warning' }); //跳轉(zhuǎn)到注冊(cè)頁面 setTimeout(() => { window.open(this.apiHost + 'userregister/userregister', '_self'); }, 1000) } else if (res.data.code == 12468) { //用戶無用戶名密碼 localStorage.setItem("token", res.data.data["JEECMS-Auth-Token"]); window.open(this.apiHost + 'uesr/enterAccount', '_parent'); } else if (res.data.code == 604) { //用戶無簡(jiǎn)歷 localStorage.setItem("token", res.data.data["JEECMS-Auth-Token"]); window.open(this.apiHost + 'uesr/fillresume', '_parent'); } else if (res.data.code == 1077) { //簡(jiǎn)歷未通過審核 localStorage.setItem("token", res.data.data["JEECMS-Auth-Token"]); window.open(this.apiHost + 'uesr/fillresume', '_parent'); } else if (res.data.code == 1075) { //簡(jiǎn)歷審核中 localStorage.setItem("token", res.data.data["JEECMS-Auth-Token"]); window.open(this.apiHost + 'uesr/audit', '_parent'); } else { this.$message.error(res.data.message); } }) } } else { //驗(yàn)證碼登錄 if (this.phone == "") { this.$message({ message: '請(qǐng)輸入手機(jī)號(hào)', type: 'warning' }); return false; } else if (!(/^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[8-9])[0-9]{8}$/.test( this.phone))) { this.$message({ message: '請(qǐng)輸入正確的手機(jī)號(hào)', type: 'warning' }); return false; } else if (this.code == "") { this.$message({ message: '請(qǐng)輸入驗(yàn)證碼', type: 'warning' }); return false; } else { request_POST('/login', qs.stringify({ identity: this.phone, captcha: this.code, loginType: 2, loginRole: 0 })).then((res) => { if (res.data.code == 200) { localStorage.setItem("token", res.data.data["JEECMS-Auth-Token"]); window.open(this.apiHost + 'uesr/resume', '_parent'); } else if (res.data.code == 12462) { this.$message({ message: '用戶未注冊(cè)', type: 'warning' }); //跳轉(zhuǎn)到注冊(cè)頁面 setTimeout(() => { window.open(this.apiHost + 'userregister/userregister', '_self'); }, 1000) } else if (res.data.code == 12468) { //用戶無用戶名密碼 localStorage.setItem("token", res.data.data["JEECMS-Auth-Token"]); window.open(this.apiHost + 'uesr/enterAccount', '_parent'); } else if (res.data.code == 604) { //用戶無簡(jiǎn)歷 localStorage.setItem("token", res.data.data["JEECMS-Auth-Token"]); window.open(this.apiHost + 'uesr/fillresume', '_parent'); } else if (res.data.code == 1077) { //簡(jiǎn)歷未通過審核 localStorage.setItem("token", res.data.data["JEECMS-Auth-Token"]); window.open(this.apiHost + 'uesr/fillresume', '_parent'); } else if (res.data.code == 1075) { //簡(jiǎn)歷審核中 localStorage.setItem("token", res.data.data["JEECMS-Auth-Token"]); window.open(this.apiHost + 'uesr/audit', '_parent'); } else { this.$message.error(res.data.message); } }) } } },
到此這篇關(guān)于vue 防止多次點(diǎn)擊的實(shí)踐的文章就介紹到這了,更多相關(guān)vue 防止多次點(diǎn)擊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Element-ui中table默認(rèn)選中toggleRowSelection問題
這篇文章主要介紹了關(guān)于Element-ui中table默認(rèn)選中toggleRowSelection問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08解決Vue3?echarts?v-show無法重新渲染的問題
這篇文章主要介紹了Vue3?echarts?v-show無法重新渲染的問題,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09vue.js+element-ui的基礎(chǔ)表單實(shí)例代碼
這篇文章主要介紹了vue.js+element-ui的基礎(chǔ)表單實(shí)例代碼,技術(shù)棧即html+vue.js+element-ui,而使用它們的方法也很簡(jiǎn)單,引入對(duì)應(yīng)的js和css文件即可,需要的朋友可以參考下2024-03-03vue父子組件傳值不能實(shí)時(shí)更新的解決方法
Vue是一個(gè)以數(shù)據(jù)驅(qū)動(dòng)、組件化的前端框架,其中組件化是Vue中較為重要的概念之一,組件之間的通信則成為Vue中較為普遍的需求,下面這篇文章主要給大家介紹了關(guān)于vue父子組件傳值不能實(shí)時(shí)更新的解決方法,需要的朋友可以參考下2023-05-05vue+element ui el-tooltip動(dòng)態(tài)顯示隱藏問題
這篇文章主要介紹了vue+element ui el-tooltip動(dòng)態(tài)顯示隱藏問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10vue el-table實(shí)現(xiàn)遞歸嵌套的示例代碼
本文主要介紹了vue el-table實(shí)現(xiàn)遞歸嵌套的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08詳解Vue單元測(cè)試Karma+Mocha學(xué)習(xí)筆記
本篇文章主要介紹了詳解Vue單元測(cè)試Karma+Mocha學(xué)習(xí)筆記,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01