HarmonyOS TextTimer示例运行不正常

TextTimer API文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-texttimer-V5#%E7%A4%BA%E4%BE%8B3

其中示例3运行不正确。时间走得很慢,并且到9.0s后就不走了。

HarmonyOS
6天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

目前beta2往后的版本做了一次不兼容更新调整。TextTimer组件的使用需要根据定义的format属性来主动适配,调整回调参数的数量级。默认值是“HH:mm:ss.SS”,其中,毫秒可使用S、SS和SSS关键字表示,分别代表100ms、10ms和1ms。此时的elapsedTime 需要*10来表示ms级,即代码中config.elapsedTime/ 100,而不在是/1000。

https://developer.huawei.com/consumer/cn/doc/harmonyos-releases-V5/changelogs-for-all-apps-b031-V5#texttimer的ontimer回调频率与参数单位调整

示例参考:

class MyTextTimerModifier implements ContentModifier<TextTimerConfiguration> {
  constructor() {
  }

  applyContent(): WrappedBuilder<[TextTimerConfiguration]> {
    return wrapBuilder(buildTextTimer)
  }
}

@Builder
function buildTextTimer(config: TextTimerConfiguration) {
  Column() {
    Stack({ alignContent: Alignment.Center }) {
      Circle({ width: 150, height: 150 })
        .fill(config.started ? (config.isCountDown ? 0xFF232323 : 0xFF717171) : 0xFF929292)
      Column() {
        Text(config.isCountDown ? "倒计时" : "正计时").fontColor(Color.White)
        Text(
          (config.isCountDown ? "剩余" : "已经过去了") + (config.isCountDown ?
            (Math.max(config.count / 1000 - config.elapsedTime / 100, 0)).toFixed(1) + "/" +
            (config.count / 1000).toFixed(0)
            : ((config.elapsedTime / 100).toFixed(0))
          ) + "秒"
        ).fontColor(Color.White)
      }
    }
  }
}

@Entry
@Component
struct TextTimerPage {
  @State count: number = 10000
  @State myTimerModifier: MyTextTimerModifier = new MyTextTimerModifier()
  countDownTextTimerController: TextTimerController = new TextTimerController()
  countUpTextTimerController: TextTimerController = new TextTimerController()

  build() {
    Row() {
      Column() {
        TextTimer({ isCountDown: true, count: this.count, controller: this.countDownTextTimerController })
          .format("HH:ss:SS")
          .contentModifier(this.myTimerModifier)
          .onTimer((utc: number, elapsedTime: number) => {
            console.info('textTimer onTimer utc is:' + utc + ', elapsedTime: ' + elapsedTime)
          })
          .margin(10)
        TextTimer({ isCountDown: false, controller: this.countUpTextTimerController })
          .contentModifier(this.myTimerModifier)
          .onTimer((utc: number, elapsedTime: number) => {
            console.info('textTimer onTimer utc is:' + utc + ', elapsedTime: ' + elapsedTime)
          })
        Row() {
          Button("start").onClick(() => {
            this.countDownTextTimerController.start()
            this.countUpTextTimerController.start()
          }).margin(10)
          Button("pause").onClick(() => {
            this.countDownTextTimerController.pause()
            this.countUpTextTimerController.pause()
          }).margin(10)
          Button("reset").onClick(() => {
            this.countDownTextTimerController.reset()
            this.countUpTextTimerController.reset()
          }).margin(10)
        }.margin(20)
      }.width('100%')
    }.height('100%')
  }
}
分享
微博
QQ
微信
回复
6天前
相关问题
HarmonyOS 旋转屏幕后显示不正常
244浏览 • 1回复 待解决
HarmonyOS Web组件预渲染加载不正常
294浏览 • 1回复 待解决
无法在卡片正常使用TextTimer组件
486浏览 • 1回复 待解决
HarmonyOS TextTimer如何设置起始时间
41浏览 • 1回复 待解决
HarmonyOS TextTimer倒计时问题
205浏览 • 1回复 待解决
HarmonyOS TextTimer 计时器使用问题
306浏览 • 1回复 待解决
HarmonyOS TextTimer如何自动开始
129浏览 • 1回复 待解决
求助一个关于TextTimer的问题 ?
976浏览 • 1回复 待解决
HarmonyOS TextTimer60秒倒计时显示成00
246浏览 • 1回复 待解决