美文网首页
鸿蒙学习笔记五:实现倒计时

鸿蒙学习笔记五:实现倒计时

作者: 馒头炖土豆 | 来源:发表于2024-06-19 09:27 被阅读0次

方法一:简单的60秒倒计时功能,代码如下

@State counter: number = 60
private timerId: number = -1
private changePerSec: number = 1


//启动倒计时
  startTimeCounter() {
    this.timerId = setInterval(() => {
      if (this.counter > 0) {
        this.counter -= this.changePerSec
      } else {
        this.stopTimeCounter()
      }
    }, 1000) //单位:毫秒
  }

//结束倒计时
  stopTimeCounter() {
    if (this.timerId > 0) {
      clearTimeout(this.timerId)
      this.timerId = -1
    }
  }

//页面消失时结束倒计时:必须要有
 aboutToDisappear(): void {
    this.stopTimeCounter()
  }

方法二:30毫秒之后进行操作

  setTimeout(() => {
      this.isScrolling = false
    }, 30) 

相关文章

网友评论

      本文标题:鸿蒙学习笔记五:实现倒计时

      本文链接:https://www.haomeiwen.com/subject/siofcjtx.html