实现步骤:
页面按钮:
Button(this.countDown===0 ? `获取验证码`:`${this.countDown}秒`)
.width(100)
.height(30)
.fontSize(13)
.onClick(()=>{
if(this.countDown === 0){
this.startCountDown()
}else{
promptAction.showToast({ message:`请${this.countDown}秒后再重新获取`,duration:2000,alignment:Alignment.Center })
}
})
倒计时:
startCountDown() {
this.countDown = 60
this.timer = setInterval(() => {
this.countDown -= 1
if (this.countDown === 0) {
clearInterval(this.timer)
}
}, 1000)
}