How to use vue to optimize SMS verification performance
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



We usually receive PDF files from the government or other agencies, some with digital signatures. After verifying the signature, we see the SignatureValid message and a green check mark. If the signature is not verified, the validity is unknown. Verifying signatures is important, let’s see how to do it in PDF. How to Verify Signatures in PDF Verifying signatures in PDF format makes it more trustworthy and the document more likely to be accepted. You can verify signatures in PDF documents in the following ways. Open the PDF in Adobe Reader Right-click the signature and select Show Signature Properties Click the Show Signer Certificate button Add the signature to the Trusted Certificates list from the Trust tab Click Verify Signature to complete the verification Let

With the popularity of smart phones, we receive a large number of text messages every day, some of which are advertising and promotional messages, and some of which are spam text messages. These text messages not only waste our time, but also occupy the space of our mobile phones. Fortunately, however, iPhones offer some features to block these annoying text messages. This article will introduce how to block text messages using iPhone. To block text messages, first open the Settings app, then scroll and tap Messages. In the information settings interface, you can see some options, including "Blocked

1. After opening WeChat, click the search icon, enter WeChat team, and click the service below to enter. 2. After entering, click the self-service tool option in the lower left corner. 3. After clicking, in the options above, click the option of unblocking/appealing for auxiliary verification.

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

PHP8 is the latest version of PHP, bringing more convenience and functionality to programmers. This version has a special focus on security and performance, and one of the noteworthy new features is the addition of verification and signing capabilities. In this article, we'll take a closer look at these new features and their uses. Verification and signing are very important security concepts in computer science. They are often used to ensure that the data transmitted is complete and authentic. Verification and signatures become even more important when dealing with online transactions and sensitive information because if someone is able to tamper with the data, it could potentially

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.
