ts-basic-components-texttimer.md 2.7 KB
Newer Older
Z
zengyawen 已提交
1 2 3
# TextTimer


4
> **NOTE**<br>
Z
zengyawen 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
> This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.


The **&lt;TextTimer&gt;** component supports custom time formats.


## Required Permissions

None


## Child Components

None


## APIs

TextTimer(options: { isCountDown?: boolean, count?: number, controller?: TextTimerController })

- Parameters
26
  | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
27
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
28
  | isCountDown | boolean | No | false | Whether to count down. |
29
  | count | number | No | 60000 | Countdown time, in milliseconds. This parameter is valid only when **isCountDown** is set to **true**.<br/>- If the specified value is less than or equal to 0, the default value is used.<br/>- If the specified value is greater than 0, the value is used. |
E
ester.zhou 已提交
30
  | controller | [TextTimerController](#texttimercontroller) | No | null | **&lt;TextTimer&gt;** controller. |
Z
zengyawen 已提交
31

E
ester.zhou 已提交
32
## Attributes
Z
zengyawen 已提交
33

E
ester.zhou 已提交
34 35 36
| Name | Type | Default Value | Description |
| -------- | -------- | -------- | -------- |
| format | string | 'hh:mm:ss.ms' | Custom format. The value must contain at least one of the following keywords: **hh**, **mm**, **ss**, and **ms**. |
Z
zengyawen 已提交
37 38


E
ester.zhou 已提交
39 40 41
## Events

| Name | Description |
Z
zengyawen 已提交
42
| -------- | -------- |
E
ester.zhou 已提交
43
| onTimer(callback: (utc: number, elapsedTime: number) =&gt; void) | Triggered when the time text changes.<br/>**utc**: currently displayed time, in milliseconds.<br/>**elapsedTime**: elapsed time of the timer, in milliseconds. |
Z
zengyawen 已提交
44 45


E
ester.zhou 已提交
46
## TextTimerController
Z
zengyawen 已提交
47

E
ester.zhou 已提交
48
Defines the controller for controlling the **&lt;TextTimer&gt;** component.
Z
zengyawen 已提交
49

E
ester.zhou 已提交
50
### Objects to Import
Z
zengyawen 已提交
51

E
ester.zhou 已提交
52 53
```
textTimerController: TextTimerController = new TextTimerController()
Z
zengyawen 已提交
54

E
ester.zhou 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
```

### start

start()

Starts the timer.

### pause

pause()

Pauses the timer. 

### reset

reset()

Resets the timer.
Z
zengyawen 已提交
74 75 76 77 78 79 80 81 82


## Example


```
@Entry
@Component
struct TextTimerExample {
E
ester.zhou 已提交
83
  textTimerController: TextTimerController = new TextTimerController()
Z
zengyawen 已提交
84 85 86 87
  @State format: string = 'hh:mm:ss.ms'

  build() {
    Column() {
E
ester.zhou 已提交
88
      TextTimer({controller: this.textTimerController})
Z
zengyawen 已提交
89 90 91 92 93 94 95 96
        .format(this.format)
        .fontColor(Color.Black)
        .fontSize(this.textSize)
        .onTimer((utc: number, elapsedTime: number) => {
          console.info('textTimer notCountDown utc is:' + utc + ', elapsedTime: ' + elapsedTime)
        })
      Row() {
        Button("start").onClick(() => {
E
ester.zhou 已提交
97
          this.textTimerController.start();
Z
zengyawen 已提交
98 99
        });
        Button("pause").onClick(() => {
E
ester.zhou 已提交
100
          this.textTimerController.pause();
Z
zengyawen 已提交
101 102
        });
        Button("reset").onClick(() => {
E
ester.zhou 已提交
103
          this.textTimerController.reset();
Z
zengyawen 已提交
104 105 106 107 108 109 110 111 112
        });
      }
    }
  }
}
```


![en-us_image_0000001257138345](figures/en-us_image_0000001257138345.gif)