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

E
ester.zhou 已提交
3
The **\<TextTimer>** component displays timing information and is controlled in text format.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5 6 7
>  **NOTE**
>
>  This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8 9 10 11


## Child Components

E
ester.zhou 已提交
12
Not supported
Z
zengyawen 已提交
13 14 15 16


## APIs

E
ester.zhou 已提交
17
TextTimer(options?: { isCountDown?: boolean, count?: number, controller?: TextTimerController })
Z
zengyawen 已提交
18

E
ester.zhou 已提交
19 20 21 22 23 24 25
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| isCountDown | boolean | No| Whether to count down.<br/>Default value: **false** |
| count | number | No| Countdown time, in milliseconds. This parameter is valid only when **isCountDown** is set to **true**.<br>If the value of **count** is less than or equal to 0, the default value is used.<br>If the value of **count** is greater than 0, it is used.<br/>Default value: **60000** |
| controller | [TextTimerController](#texttimercontroller) | No| **\<TextTimer>** controller.|
Z
zengyawen 已提交
26

E
ester.zhou 已提交
27
## Attributes
Z
zengyawen 已提交
28

E
ester.zhou 已提交
29 30 31
| Name| Type| Description|
| -------- | -------- | -------- |
| format | string | Custom format. The value must contain at least one of the following keywords: **hh**, **mm**, **ss**, and **ms**.<br>Default value: **'hh:mm:ss.ms'** |
Z
zengyawen 已提交
32 33


E
ester.zhou 已提交
34 35
## Events

E
ester.zhou 已提交
36
| Name| Description|
Z
zengyawen 已提交
37
| -------- | -------- |
E
ester.zhou 已提交
38
| onTimer(event: (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 已提交
39 40


E
ester.zhou 已提交
41
## TextTimerController
Z
zengyawen 已提交
42

E
ester.zhou 已提交
43
Defines the controller for controlling the **\<TextTimer>** component. A **\<TextTimer>** component can be bound to only one controller.
Z
zengyawen 已提交
44

E
ester.zhou 已提交
45
### Objects to Import
Z
zengyawen 已提交
46

E
ester.zhou 已提交
47 48
```
textTimerController: TextTimerController = new TextTimerController()
Z
zengyawen 已提交
49

E
ester.zhou 已提交
50 51 52 53 54 55 56 57 58 59 60 61
```

### start

start()

Starts the timer.

### pause

pause()

E
ester.zhou 已提交
62
Pauses the timer.
E
ester.zhou 已提交
63 64 65 66 67 68

### reset

reset()

Resets the timer.
Z
zengyawen 已提交
69 70 71 72


## Example

E
ester.zhou 已提交
73 74
```ts
// xxx.ets
Z
zengyawen 已提交
75 76 77
@Entry
@Component
struct TextTimerExample {
E
ester.zhou 已提交
78
  textTimerController: TextTimerController = new TextTimerController()
Z
zengyawen 已提交
79 80 81 82
  @State format: string = 'hh:mm:ss.ms'

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


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