Home > Web Front-end > JS Tutorial > body text

js implementation of SMS verification code countdown example sharing

小云云
Release: 2018-03-10 16:03:19
Original
1484 people have browsed it


#1. After the target obtains the verification code, the button displays a countdown to resend, and the button becomes available again after the countdown ends. This article mainly shares with you an example of using js to implement SMS verification code countdown. I hope it can help you.

2. The two key APIs used

are executed every second: setInterval

##Execute after the specified time: setTimeout

3. Code
<span style="font-size: 14px;">function createTimer(time,interval,timeout){<br/><br/>  let hasStartTime = 0//已经走了的时间<br/>  interval(hasStartTime)//立即执行一次<br/>  let _interval = setInterval(()=>{<br/>    hasStartTime = hasStartTime+1000<br/>    interval(hasStartTime)<br/>  }, 1000)<br/>  setTimeout(()=>{<br/>    clearInterval(_interval)<br/>    timeout()<br/>  }, time || 60000)<br/>}<br/></span>
Copy after login

where time is the total time set and interval is each The operation is performed in seconds, and timeout is the operation performed after the time is over. Remember to cancel the operations performed every second after the countdown is over.

The caller only needs to pass in the content to be refreshed every second. For example

<span style="font-size: 14px;">let time = 10000<br/>createTimer(time,timeStart=>{<br/>    let btn_text = `重新发送(${(time- timeStart)/1000}s)`<br/>    let btn_lock = true<br/>  },()=>{<br/>    let btn_text = `重新发送`,<br/>    let btn_lock = false<br/>  })<br/></span>
Copy after login
Related recommendations:

js implements SMS verification code countdown function

Develop SMS verification code sending function in Laravel "Specification" (picture)

JS implementation of sample code sharing to obtain SMS verification code and countdown function when user registers

The above is the detailed content of js implementation of SMS verification code countdown example sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template