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