1e41f4b71Sopenharmony_ci# Timer
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThe **Timer** module provides basic timer capabilities. You can use the APIs of this module to execute functions at the specified time.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **NOTE**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> 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.
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci> When a timer is used on the UI, the timer triggering mechanism is controlled by the underlying principles of the UI. As such, when the UI transitions to the background, the timer will be frozen.
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci## setTimeout
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_cisetTimeout(handler: Function | string, delay?: number, ...arguments: any[]): number
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ciSets a timer for the system to call a function after the timer goes off. 
16e41f4b71Sopenharmony_ciThe timer is automatically deleted after the callback is executed, and can be manually deleted by calling the **clearTimeout** API.
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci**Parameters**
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
23e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
24e41f4b71Sopenharmony_ci| 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.|
25e41f4b71Sopenharmony_ci| 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.|
26e41f4b71Sopenharmony_ci| ...arguments | any[] | No| Additional parameters to pass to the handler after the timer goes off.|
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci**Return value**
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci| Type| Description|
31e41f4b71Sopenharmony_ci| -------- | -------- |
32e41f4b71Sopenharmony_ci| number | ID of the timer. The timer ID is shared by processes and is an integer starting from 0 in ascending order.|
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci**Example**
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci  ```ts
37e41f4b71Sopenharmony_ci  setTimeout(() => {
38e41f4b71Sopenharmony_ci    console.log('delay 1s');
39e41f4b71Sopenharmony_ci  }, 1000);
40e41f4b71Sopenharmony_ci  ```
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci## clearTimeout
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ciclearTimeout(timeoutID?: number): void
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ciCancels the repeating timer set via **setTimeout()**.
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ciThe timer object is stored in the thread that creates the timer and must be deleted in that thread.
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci**Parameters**
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
56e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
57e41f4b71Sopenharmony_ci| timeoutID | number | No| ID of the timer to cancel, which is returned by **setTimeout()** If this parameter is omitted, no timer is canceled.|
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci**Example**
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci  ```js
62e41f4b71Sopenharmony_ci  let timeoutID = setTimeout(() => {
63e41f4b71Sopenharmony_ci    console.log('do after 1s delay.');
64e41f4b71Sopenharmony_ci  }, 1000);
65e41f4b71Sopenharmony_ci  clearTimeout(timeoutID);
66e41f4b71Sopenharmony_ci  ```
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci## setInterval
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_cisetInterval(handler: Function | string, delay: number, ...arguments: any[]): number
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ciSets a repeating timer for the system to repeatedly call a function at a fixed interval.
74e41f4b71Sopenharmony_ciThe timer can only be manually deleted by calling the **clearInterval** API.
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci**Parameters**
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
81e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
82e41f4b71Sopenharmony_ci| handler | Function \| string | Yes| Function to be called repeatedly. If the type is string, error information is printed and no other processing is performed.|
83e41f4b71Sopenharmony_ci| delay | number | Yes| Number of milliseconds delayed before the execution.|
84e41f4b71Sopenharmony_ci| ...arguments | any[] | No| Additional parameters to pass to the handler after the timer goes off.|
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci**Return value**
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci| Type| Description|
89e41f4b71Sopenharmony_ci| -------- | -------- |
90e41f4b71Sopenharmony_ci| number | ID of the timer. The timer ID is shared by processes and is an integer starting from 0 in ascending order.|
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci**Example**
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci  ```js
95e41f4b71Sopenharmony_ci  setInterval(() => {
96e41f4b71Sopenharmony_ci    console.log('do every 1s.');
97e41f4b71Sopenharmony_ci  }, 1000);
98e41f4b71Sopenharmony_ci  ```
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci## clearInterval
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ciclearInterval(intervalID?: number): void
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ciCancels the repeating timer set via **setInterval()**.
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ciThe timer object is stored in the thread that creates the timer and must be deleted in that thread.
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ci**Parameters**
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
114e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
115e41f4b71Sopenharmony_ci| intervalID | number | No| ID of the repeating timer to cancel, which is returned by **setInterval()**. If this parameter is omitted, no timer is canceled.|
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci**Example**
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci  ```js
120e41f4b71Sopenharmony_ci  let intervalID = setInterval(() => {
121e41f4b71Sopenharmony_ci    console.log('do every 1s.');
122e41f4b71Sopenharmony_ci  }, 1000);
123e41f4b71Sopenharmony_ci  clearInterval(intervalID);
124e41f4b71Sopenharmony_ci  ```
125