当前位置:   article > 正文

鸿蒙(Arkts)定时器_arkts定时器

arkts定时器
  1. // 自定义计时器/倒计时组件
  2. @Component
  3. struct TimerComponent {
  4. @State counter: number = 0
  5. private changePerSec: number = -1
  6. private showInColor: Color = Color.Black
  7. private timerId: number = -1
  8. build() {
  9. Text(`${this.counter}sec`)
  10. .fontColor(this.showInColor)
  11. .fontSize(20)
  12. .margin(20)
  13. }
  14. aboutToAppear() {
  15. this.timerId = setInterval(() => {
  16. this.counter += this.changePerSec
  17. }, 1000)
  18. }
  19. aboutToDisappear() {
  20. if (this.timerId > 0) {
  21. clearTimeout(this.timerId)
  22. this.timerId = -1
  23. }
  24. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号