vue 防止多次點(diǎn)擊的實(shí)踐
一般點(diǎn)擊事件會(huì)分不同的情況進(jìn)行消息提醒,如果不做處理,短短幾秒彈出很多條提示信息,就會(huì)很煩,比如:
那要怎么控制這個(gè)提示信息只能出現(xiàn)單條呢
再點(diǎn)擊事件的方法最前面加上
定義變量hasRemind來(lái)控制是否執(zhí)行點(diǎn)擊事件里的相應(yīng)操作
當(dāng)用戶第一次點(diǎn)擊的時(shí)候,hasRemind = false,此時(shí),進(jìn)入到第二個(gè)if語(yǔ)句,講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è)頁(yè)面setTimeout(() => { window.open(this.apiHost + ’userregister/userregister’,’_self’);}, 1000) } else if (res.data.code == 12468) { //用戶無(wú)用戶名密碼localStorage.setItem('token', res.data.data['JEECMS-Auth-Token']);window.open(this.apiHost + ’uesr/enterAccount’, ’_parent’); } else if (res.data.code == 604) { //用戶無(wú)簡(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)歷未通過(guò)審核localStorage.setItem('token', res.data.data['JEECMS-Auth-Token']);window.open(this.apiHost + ’uesr/fillresume’, ’_parent’); } else if (res.data.code == 1075) { //簡(jiǎn)歷審核中l(wèi)ocalStorage.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è)頁(yè)面setTimeout(() => { window.open(this.apiHost + ’userregister/userregister’,’_self’);}, 1000) } else if (res.data.code == 12468) { //用戶無(wú)用戶名密碼localStorage.setItem('token', res.data.data['JEECMS-Auth-Token']);window.open(this.apiHost + ’uesr/enterAccount’, ’_parent’); } else if (res.data.code == 604) { //用戶無(wú)簡(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)歷未通過(guò)審核localStorage.setItem('token', res.data.data['JEECMS-Auth-Token']);window.open(this.apiHost + ’uesr/fillresume’, ’_parent’); } else if (res.data.code == 1075) { //簡(jiǎn)歷審核中l(wèi)ocalStorage.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)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. html小技巧之td,div標(biāo)簽里內(nèi)容不換行2. HTML DOM setInterval和clearInterval方法案例詳解3. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?4. webpack高級(jí)配置與優(yōu)化詳解5. HTML <!DOCTYPE> 標(biāo)簽6. CSS3中Transition屬性詳解以及示例分享7. 概述IE和SQL2k開(kāi)發(fā)一個(gè)XML聊天程序8. XML入門的常見(jiàn)問(wèn)題(一)9. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享10. ASP腳本組件實(shí)現(xiàn)服務(wù)器重啟
