• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Timer
2
3The **Timer** module provides basic timer capabilities. You can use the APIs of this module to execute functions at the specified time.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## setTimeout
10
11setTimeout(handler: Function | string, delay?: number, ...arguments: any[]): number
12
13Sets a timer for the system to call a function after the timer goes off.
14
15The timer is automatically deleted after the callback is executed, and can be manually deleted by calling the **clearTimeout** API.
16
17**System capability**: SystemCapability.ArkUI.ArkUI.Full
18
19**Parameters**
20
21| Name| Type| Mandatory| Description|
22| -------- | -------- | -------- | -------- |
23| handler | Function \| string | Yes| Function to be called after the timer goes off. If the type is string, error information is printed and no other processing is performed.|
24| delay | number | No| Number of milliseconds delayed before the execution. If this parameter is left empty, the default value **0** is used, which means that the execution starts immediately or as soon as possible.|
25| ...arguments | any[] | No| Additional parameters to pass to the handler after the timer goes off.|
26
27**Return value**
28
29| Type| Description|
30| -------- | -------- |
31| number | ID of the timer. The value is an integer.|
32
33**Example**
34
35  ```ts
36  setTimeout(() => {
37    console.log('delay 1s');
38  }, 1000);
39  ```
40
41
42## clearTimeout
43
44clearTimeout(timeoutID?: number): void
45
46Cancels the repeating timer set via **setTimeout()**.
47
48**System capability**: SystemCapability.ArkUI.ArkUI.Full
49
50**Parameters**
51
52| Name| Type| Mandatory| Description|
53| -------- | -------- | -------- | -------- |
54| timeoutID | number | No| ID of the timer to cancel, which is returned by **setTimeout()** If this parameter is omitted, no timer is canceled.|
55
56**Example**
57
58  ```js
59  let timeoutID = setTimeout(() => {
60    console.log('do after 1s delay.');
61  }, 1000);
62  clearTimeout(timeoutID);
63  ```
64
65
66## setInterval
67
68setInterval(handler: Function | string, delay: number, ...arguments: any[]): number
69
70Sets a repeating timer for the system to repeatedly call a function at a fixed interval.
71
72The timer can only be manually deleted by calling the **clearInterval** API.
73
74**System capability**: SystemCapability.ArkUI.ArkUI.Full
75
76**Parameters**
77
78| Name| Type| Mandatory| Description|
79| -------- | -------- | -------- | -------- |
80| handler | Function \| string | Yes| Function to be called repeatedly. If the type is string, error information is printed and no other processing is performed.|
81| delay | number | Yes| Number of milliseconds delayed before the execution.|
82| ...arguments | any[] | No| Additional parameters to pass to the handler after the timer goes off.|
83
84**Return value**
85
86| Type| Description|
87| -------- | -------- |
88| number | ID of the timer. The value is an integer.|
89
90**Example**
91
92  ```js
93  setInterval(() => {
94    console.log('do every 1s.');
95  }, 1000);
96  ```
97
98
99## clearInterval
100
101clearInterval(intervalID?: number): void
102
103Cancels the repeating timer set via **setInterval()**.
104
105**System capability**: SystemCapability.ArkUI.ArkUI.Full
106
107**Parameters**
108
109| Name| Type| Mandatory| Description|
110| -------- | -------- | -------- | -------- |
111| intervalID | number | No| ID of the repeating timer to cancel, which is returned by **setInterval()**. If this parameter is omitted, no timer is canceled.|
112
113**Example**
114
115  ```js
116  let intervalID = setInterval(() => {
117    console.log('do every 1s.');
118  }, 1000);
119  clearInterval(intervalID);
120  ```
121