• 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.|
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 | Array<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  export default {
35    setTimeOut() {
36      var timeoutID = setTimeout(function() {
37        console.log('delay 1s');
38      }, 1000);
39    }
40  }
41  ```
42
43
44## clearTimeout
45
46clearTimeout(timeoutID: number): void
47
48Cancels the timer created via **setTimeout()**.
49
50**System capability**: SystemCapability.ArkUI.ArkUI.Full
51
52**Parameters**
53
54| Name| Type| Mandatory| Description|
55| -------- | -------- | -------- | -------- |
56| timeoutID | number | Yes| ID of the timer to cancel, which is returned by **setTimeout()**|
57
58**Example**
59
60  ```js
61  export default {
62    clearTimeOut() {
63      var timeoutID = setTimeout(function() {
64        console.log('do after 1s delay.');
65      }, 1000);
66      clearTimeout(timeoutID);
67    }
68  }
69  ```
70
71
72## setInterval
73
74setInterval(handler: Function | string, delay: number, ...arguments: any[]): number
75
76Sets a repeating timer for the system to repeatedly call a function at a fixed interval.
77
78**System capability**: SystemCapability.ArkUI.ArkUI.Full
79
80**Parameters**
81
82| Name| Type| Mandatory| Description|
83| -------- | -------- | -------- | -------- |
84| handler | Function \| string | Yes| Function to be called repeatedly.|
85| delay | number | Yes| Number of milliseconds delayed before the execution.|
86| ...arguments | Array<any> | No| Additional parameters to pass to the handler after the timer goes off.|
87
88**Return value**
89
90| Type| Description|
91| -------- | -------- |
92| number | ID of the repeating timer.|
93
94**Example**
95
96  ```js
97  export default {
98    setInterval() {
99      var intervalID = setInterval(function() {
100        console.log('do very 1s.');
101      }, 1000);
102    }
103  }
104  ```
105
106
107## clearInterval
108
109clearInterval(intervalID: number): void
110
111Cancels the repeating timer set via **setInterval()**.
112
113**System capability**: SystemCapability.ArkUI.ArkUI.Full
114
115**Parameters**
116
117| Name| Type| Mandatory| Description|
118| -------- | -------- | -------- | -------- |
119| intervalID | number | Yes| ID of the repeating timer to cancel, which is returned by **setInterval()**.|
120
121**Example**
122
123  ```js
124  export default {
125    clearInterval() {
126      var intervalID = setInterval(function() {
127        console.log('do very 1s.');
128      }, 1000);
129      clearInterval(intervalID);
130    }
131  }
132  ```
133