This time I will bring you how to use vue to implement SMS verification Performance optimization, what are the precautions for using vue to implement SMS verification performance optimization, the following is a practical case, let's come together take a look.
Usually when we register in projects, we often use the SMS verification function, but now many SMS verifications have the following problems, for example, when the SMS verification time is 60s,
1. When the button is clicked and the countdown has not reached 60 seconds, refresh the browser and the Verification Code button can be clicked again
2. When the button is clicked The countdown starts. For example, I close the browser at 50s. After 5s, I open it. At this time, the countdown time should be about 45s, but when I reopen the browser, the button can be clicked again.
In order to solve the above two problems, you need to write the time into localstorage. When the page is opened, go to localstorage to get it. I will paste my solution here, because the previous few There is a vue project that uses this method, so I will write a vue method here
HTML code in the component:
<p class="mtui-cellft" @click="getCode"> <button class="mtui-vcode-btn mtui-text-center" v-if="flag">获取短信</button> <button class="mtui-vcode-btn mtui-text-center" v-if="!flag">剩余{{second}}s</button> </p>
Here comes the key point
Define several variables that need to be used in data:
second: 60, flag: true, timer: null // 该变量是用来记录定时器的,防止点击的时候触发多个setInterval
How to obtain SMS verification:
getCode() { let that = this; if (that.flag) { that.flag = false; let interval = window.setInterval(function() { that.setStorage(that.second); if (that.second-- <= 0) { that.second = 60; that.flag = true; window.clearInterval(interval); } }, 1000); } }
Write and read localstorage:
setStorage(parm) { localStorage.setItem("dalay", parm); localStorage.setItem("_time", new Date().getTime()); }, getStorage() { let localDelay = {}; localDelay.delay = localStorage.getItem("dalay"); localDelay.sec = localStorage.getItem("_time"); return localDelay; }
Prevent page refresh from invalidating the verification code:
judgeCode() { let that = this; let localDelay = that.getStorage(); let secTime = parseInt( (new Date().getTime() - localDelay.sec) / 1000 ); console.log(localDelay); if (secTime > localDelay.delay) { that.flag = true; console.log("已过期"); } else { that.flag = false; let _delay = localDelay.delay - secTime; that.second = _delay; that.timer = setInterval(function() { if (_delay > 1) { _delay--; that.setStorage(_delay); that.second = _delay; that.flag = false; } else { // 此处赋值时为了让浏览器打开的时候,直接就显示剩余的时间 that.flag = true; window.clearInterval(that.timer); } }, 1000); } }
Then call the judgeCode() method in the life hook (mounted) after the html mounting page is completed to achieve this function
I believe After reading the case in this article, you have mastered the method. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
##How to use vue to determine the class of dom
The above is the detailed content of How to use vue to optimize SMS verification performance. For more information, please follow other related articles on the PHP Chinese website!