React Native implements verification code countdown function
This time I will bring you React Native to implement Verification codeCountdown function, what are the precautions for React Native to implement verification code countdown function , the following is a practical case, let’s take a look.
Because I used timer before and did not calculate the current time. Every time I exited the program, the timer never ran. This tool class simply solves the problem of exiting the program from the background and timing. If the bug does not work, then just upload the code~~
/** * Created by zhuang.haipeng on 2017.9.11 * 广告倒计时,验证码倒计时工具类 * 用法: //60 * 1000 为60秒 , 60 * 60 * 100 为60分钟 ... * let countdownDate = new Date(new Date().getTime() + 60 * 1000) * CountdownUtil.settimer(countdownDate, (time) => { * Console.log(time) --> {years: 0, days: 0, hours: 0, min: 0, sec: 49, millisec: 0} * } * 切记: 在应用工具类的页面退出的时候, 调用CountdownUtil.stop() 清除定时器,以免内存爆了 */ export default class CountdownUtil { /** 定时器 */ interval = null; /** * 创建定时器 */ static settimer(countdownDate, callbak) { this.interval = setInterval(() => { let time = this.getDateData(countdownDate) callbak && callbak(time) }, 1000) } /** * 侄计时计算 --> 通过此方法计算,可以解决应用退出后台的时候,定时器不走 * @param countdownDate * @return {*} */ static getDateData(countdownDate) { let diff = (Date.parse(new Date(countdownDate)) - Date.parse(new Date)) / 1000; if (diff <= 0) { this.stop() // 倒计时为0的时候, 将计时器清除 return 0; } const timeLeft = { years: 0, days: 0, hours: 0, min: 0, sec: 0, millisec: 0, }; if (diff >= (365.25 * 86400)) { timeLeft.years = Math.floor(diff / (365.25 * 86400)); diff -= timeLeft.years * 365.25 * 86400; } if (diff >= 86400) { timeLeft.days = Math.floor(diff / 86400); diff -= timeLeft.days * 86400; } if (diff >= 3600) { timeLeft.hours = Math.floor(diff / 3600); diff -= timeLeft.hours * 3600; } if (diff >= 60) { timeLeft.min = Math.floor(diff / 60); diff -= timeLeft.min * 60; } timeLeft.sec = diff; return timeLeft; } /** * 数字补零 --> 例: 00时01分59秒 * @param num * @param length * @return {*} */ static leadingZeros(num, length = null) { let length_ = length; let num_ = num; if (length_ === null) { length_ = 2; } num_ = String(num_); while (num_.length < length_) { num_ = '0' + num_; } return num_; } /** 清除定时器 */ static stop() { clearInterval(this.interval); } };
Use callback to pass the converted time countdown. You can print the time object returned by callbak
Here is a simple example of the verification code countdown:
Thoughts:
1. First set the state machine isSentVerify to be true by default to send the verification code
2. After clicking, reset the state machine isSentVerify to false to prevent the user from clicking again to send a network request
3. Declare the countdown time (only here It can only be declared when you click it. If you are in componentDidMount, the time will start as soon as you enter)
4. Set the countdown after the request is successful. If time.sec > 0, set the time, otherwise the text will be set. Set it to "Reacquire"
5. Then determine the text as "Reacquire", and then set the state machine isSentVerify to true, so that the user can send the verification code again after the countdown is over.
6. When the network request fails, set isSentVerify to true at the catch point so that the user can obtain the verification code again
if (this.state.isSentVerify === true) { // 倒计时时间 let countdownDate = new Date(new Date().getTime() + 60 * 1000) // 点击之后验证码不能发送网络请求 this.setState({ isSentVerify: false }); Api.userRegisterCheckCode(this.state.phoneText) .then( (data) => { // 倒计时时间 CountdownUtil.settimer(countdownDate, (time) => { this.setState({ timerTitle: time.sec > 0 ? time.sec + 's' : '重新获取' }, () => { if (this.state.timerTitle == "重新获取") { this.setState({ isSentVerify: true }) } }) }) } ).catch((err) => { this.setState({ isSentVerify: true, }) }); }
When exiting the page, remember to destroy the timer
componentWillUnmount() { CountdownUtil.stop() }
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
The above is the detailed content of React Native implements verification code countdown function. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











C++ is a widely used programming language that is very convenient and practical in writing countdown programs. Countdown program is a common application that can provide us with very precise time calculation and countdown functions. This article will introduce how to use C++ to write a simple countdown program. The key to implementing a countdown program is to use a timer to calculate the passage of time. In C++, we can use the functions in the time.h header file to implement the timer function. The following is the code for a simple countdown program

ReactRouter User Guide: How to Implement Front-End Routing Control With the popularity of single-page applications, front-end routing has become an important part that cannot be ignored. As the most popular routing library in the React ecosystem, ReactRouter provides rich functions and easy-to-use APIs, making the implementation of front-end routing very simple and flexible. This article will introduce how to use ReactRouter and provide some specific code examples. To install ReactRouter first, we need

PHP, Vue and React: How to choose the most suitable front-end framework? With the continuous development of Internet technology, front-end frameworks play a vital role in Web development. PHP, Vue and React are three representative front-end frameworks, each with its own unique characteristics and advantages. When choosing which front-end framework to use, developers need to make an informed decision based on project needs, team skills, and personal preferences. This article will compare the characteristics and uses of the three front-end frameworks PHP, Vue and React.

Integration of Java framework and React framework: Steps: Set up the back-end Java framework. Create project structure. Configure build tools. Create React applications. Write REST API endpoints. Configure the communication mechanism. Practical case (SpringBoot+React): Java code: Define RESTfulAPI controller. React code: Get and display the data returned by the API.

In win10, the boot countdown is enabled by default. When we turn on the computer, we will see a countdown interface, usually a 10-second countdown. Within this time, we can choose whether to continue booting or perform some other operations. Although the boot countdown brings some convenience to our system, it may also cause trouble in some cases. I want to cancel the display, but I don’t know how to do it. This article brings you how to cancel the countdown of several seconds after booting up Win10. Understand the win10 boot countdown. In win10, the boot countdown is enabled by default. When we turn on the computer, we will see a countdown interface, usually a 10-second countdown. Within this time, we can choose whether to continue booting or proceed

How to use CSS to create a countdown effect. The countdown effect is a common function in web development. It can provide users with a dynamic effect of countdown and give people a sense of urgency and expectation. This article will introduce how to use CSS to achieve the countdown effect, and give detailed implementation steps and code examples. The implementation steps are as follows: Step 1: HTML structure construction First, create a div container in HTML to wrap the countdown content. For example: <divclass="countd

Vue.js is suitable for small and medium-sized projects and fast iterations, while React is suitable for large and complex applications. 1) Vue.js is easy to use and is suitable for situations where the team is insufficient or the project scale is small. 2) React has a richer ecosystem and is suitable for projects with high performance and complex functional needs.

React has closures such as event handling functions, useEffect and useCallback, higher-order components, etc. Detailed introduction: 1. Event handling function closure: In React, when we define an event handling function in a component, the function will form a closure and can access the status and properties within the component scope. In this way, the state and properties of the component can be used in the event processing function to implement interactive logic; 2. Closures in useEffect and useCallback, etc.
