鸿蒙开发验证码倒计时功能

122 阅读1分钟
鸿蒙开发验证码倒计时功能

验证码倒计时60s还蛮简单的,就是用间隔时间弄。但时间的累计和相减得自己弄。

一、思路

用setInterval

二、效果图

在这里插入图片描述 在这里插入图片描述

三、关键代码如下:
startCountDown() {
    this.timer  = setInterval(()=>{
      if (this.stop_timer) {
        this.btnCodeTip = getString($r('app.string.get_code'))
        this.currentDownTime = 0
        this.btnCodeTipEnable = true
        clearInterval(this.timer)
      } else {
        try {
          this.currentDownTime = this.currentDownTime + 1;
          if (this.currentDownTime === 60) {
            this.stop_timer = true
          } else {
            this.btnCodeTip = (60-this.currentDownTime)+"s"
            this.btnCodeTipEnable = false
          }
        } catch (e) {
        }
      }
    }, 1000)
  }
四、项目demo结构图

在这里插入图片描述 有需要完整demo源码的私信我