let countDownTimer = null
export async function countDown(onBefor = () => { }, onUpdate = (second) => { }, second = 60 ,endContext) {
let _second = second
if (countDownTimer)return
if(onBefor instanceof Promise){
if(!await onBefor)return
}else{
if(!onBefor())return
}
countDownTimer = setInterval(() => {
_second--
onUpdate(_second,_second == 0 && endContext)
if (_second == 0) {
clearInterval(countDownTimer)
countDownTimer = null
_second = second
return
}
}, 1000)
return true
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
import {countDown} from "utils.js"
const getCode = new Promise(r=>{
setTimeout(()=>{
r(true)
},2000)
})
const update = (second,endText)=>{
if(endText)return this.sendCodeText = endText
this.sendCodeText = `${second}后重试`
}
countDown(getCode,update,5,"发送验证码")