Ionic learning diary implements Verification Code countdown, ionic Verification Code

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.