• 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
15**System capability**: SystemCapability.ArkUI.ArkUI.Full
16
17**Parameters**
18
19| Name| Type| Mandatory| Description|
20| -------- | -------- | -------- | -------- |
21| 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.|
22| 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.|
23| ...arguments | any[] | No| Additional parameters to pass to the handler after the timer goes off.|
24
25**Return value**
26
27| Type| Description|
28| -------- | -------- |
29| number | Timer ID.|
30
31**Example**
32
33  ```js
34  setTimeout(function() {
35    console.log('delay 1s');
36  }, 1000);
37  ```
38
39
40## clearTimeout
41
42clearTimeout(timeoutID?: number): void
43
44Cancels the timer created via **setTimeout()**.
45
46**System capability**: SystemCapability.ArkUI.ArkUI.Full
47
48**Parameters**
49
50| Name| Type| Mandatory| Description|
51| -------- | -------- | -------- | -------- |
52| timeoutID | number | No| ID of the timer to cancel, which is returned by **setTimeout()** If this parameter is omitted, no timer is canceled.|
53
54**Example**
55
56  ```js
57  let timeoutID = setTimeout(function() {
58    console.log('do after 1s delay.');
59  }, 1000);
60  clearTimeout(timeoutID);
61  ```
62
63
64## setInterval
65
66setInterval(handler: Function | string, delay: number, ...arguments: any[]): number
67
68Sets a repeating timer for the system to repeatedly call a function at a fixed interval.
69
70**System capability**: SystemCapability.ArkUI.ArkUI.Full
71
72**Parameters**
73
74| Name| Type| Mandatory| Description|
75| -------- | -------- | -------- | -------- |
76| handler | Function \| string | Yes| Function to be called repeatedly. If the type is string, error information is printed and no other processing is performed.|
77| delay | number | Yes| Number of milliseconds delayed before the execution.|
78| ...arguments | any[] | No| Additional parameters to pass to the handler after the timer goes off.|
79
80**Return value**
81
82| Type| Description|
83| -------- | -------- |
84| number | Timer ID.|
85
86**Example**
87
88  ```js
89  setInterval(function() {
90    console.log('do every 1s.');
91  }, 1000);
92  ```
93
94
95## clearInterval
96
97clearInterval(intervalID?: number): void
98
99Cancels the repeating timer set via **setInterval()**.
100
101**System capability**: SystemCapability.ArkUI.ArkUI.Full
102
103**Parameters**
104
105| Name| Type| Mandatory| Description|
106| -------- | -------- | -------- | -------- |
107| intervalID | number | No| ID of the repeating timer to cancel, which is returned by **setInterval()**. If this parameter is omitted, no timer is canceled.|
108
109**Example**
110
111  ```js
112  let intervalID = setInterval(function() {
113    console.log('do every 1s.');
114  }, 1000);
115  clearInterval(intervalID);
116  ```
117