Home > Article > Web Front-end > js implementation of SMS verification code countdown example sharing
are executed every second: setInterval
##Execute after the specified time: setTimeout
<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>
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>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!