赞
踩
- // 自定义计时器/倒计时组件
- @Component
- struct TimerComponent {
- @State counter: number = 0
- private changePerSec: number = -1
- private showInColor: Color = Color.Black
- private timerId: number = -1
-
- build() {
- Text(`${this.counter}sec`)
- .fontColor(this.showInColor)
- .fontSize(20)
- .margin(20)
- }
-
- aboutToAppear() {
- this.timerId = setInterval(() => {
- this.counter += this.changePerSec
- }, 1000)
- }
-
- aboutToDisappear() {
- if (this.timerId > 0) {
- clearTimeout(this.timerId)
- this.timerId = -1
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。