Ionic learning diary implements Verification Code countdown, ionic Verification Code
Preface
If you want to create an app, it will certainly involve this function. Therefore, I have found many documents from my predecessors on the Internet and found the most suitable and easy-to-understand way to implement this function, write this diary for future review and others to learn
Ideas
During User Registration, many apps now choose to bind mobile phone numbers for registration. This is a very good idea, which facilitates user operations and follows the real-name system. When designing buttons, you need to display the button next to the Verification code you entered. After the user clicks it, the countdown starts and the button becomes unclickable.
Before clicking
After clicking
In this diary, only files on one page are involved, including html, ts, and scss (my page name is reg, which can be adjusted according to your own situation)
Reg. ts defines the information that can be obtained in html.
// Verification Code countdown verifyCode: any = {verifyCodeTips: "Get verification code", countdown: 60, disable: true}
Reg.html Design Layout
The image above is designed by myself. Here, only the key code is used.
Copy codeThe Code is as follows:
<Button item-right ion-button clear (click) = "getCode ()" [disabled] = "! VerifyCode. disable ">{{ verifyCode. verifyCodeTips }}</button>
Click the getCode () event and set whether the [disabled] button can be clicked. The boolean value is used to determine whether verifyCode. verifyCodeTips is displayed, that is, text information and subsequent countdown.
Reg. ts addition method and countdown Processing
When a button is clicked, The getCode () method is triggered. After this method is triggered, the disable value is changed to false, and the button is set to unclickable, And the settime method is triggered.
GetCode () {// click the button to start countdown this. verifyCode. disable = false; this. settime ();}
Settime () implements the countdown Function
// Countdown settime () {if (this. verifyCode. countdown = 1) {this. verifyCode. countdown = 60; this. verifyCode. verifyCodeTips = "get verification code"; this. verifyCode. disable = true; return;} else {this. verifyCode. countdown --;} this. verifyCode. verifyCodeTips = "get again" + this. verifyCode. countdown + "second"; setTimeout () => {this. verifyCode. verifyCodeTips = "get again" + this. verifyCode. countdown + "seconds"; this. settime () ;}, 1000 );}
Use the simple countdown function to subtract 1 from every second. It is important to determine whether the counter is 1. When it is 1, reinitialize the three verifyCode information.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.