js-apis-timer.md 3.3 KB
Newer Older
1
# Timer
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3 4 5 6 7
The **Timer** module provides basic timer capabilities. You can use the APIs of this module to execute functions at the specified time.

> **NOTE**
>
> 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.
Z
zengyawen 已提交
8

E
ester.zhou 已提交
9
## setTimeout
Z
zengyawen 已提交
10

E
ester.zhou 已提交
11
setTimeout(handler[,delay[,…args]]): number
Z
zengyawen 已提交
12 13 14

Sets a timer for the system to call a function after the timer goes off.

E
ester.zhou 已提交
15 16
**System capability**: SystemCapability.ArkUI.ArkUI.Full

E
ester.zhou 已提交
17
**Parameters**
18

E
ester.zhou 已提交
19 20 21 22 23
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| handler | Function | Yes| Function to be called after the timer goes off.|
| 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.|
| ...args | Array<any> | No| Additional parameters to pass to the handler after the timer goes off.|
24

E
ester.zhou 已提交
25
**Return value**
26

E
ester.zhou 已提交
27 28 29
| Type| Description|
| -------- | -------- |
| number | Timer ID.|
30

E
ester.zhou 已提交
31
**Example**
32

E
ester.zhou 已提交
33 34 35 36 37 38 39
  ```js
  export default {    
    setTimeOut() {        
      var timeoutID = setTimeout(function() {            
        console.log('delay 1s');
      }, 1000);    
    }
40
  }
E
ester.zhou 已提交
41
  ```
E
ester.zhou 已提交
42

Z
zengyawen 已提交
43

44
## clearTimeout
Z
zengyawen 已提交
45

46
clearTimeout(timeoutID: number): void
Z
zengyawen 已提交
47

48
Cancels the timer created via **setTimeout()**.
Z
zengyawen 已提交
49

E
ester.zhou 已提交
50 51
**System capability**: SystemCapability.ArkUI.ArkUI.Full

E
ester.zhou 已提交
52
**Parameters**
53

E
ester.zhou 已提交
54 55 56
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| timeoutID | number | Yes| ID of the timer to cancel, which is returned by **setTimeout()**|
57

E
ester.zhou 已提交
58
**Example**
59

E
ester.zhou 已提交
60 61 62 63 64 65 66 67
  ```js
  export default {    
    clearTimeOut() {        
      var timeoutID = setTimeout(function() {            
        console.log('do after 1s delay.');        
      }, 1000);        
      clearTimeout(timeoutID);    
    }
68
  }
E
ester.zhou 已提交
69
  ```
E
ester.zhou 已提交
70

71 72 73 74 75 76 77

## setInterval

setInterval(handler[, delay[, ...args]]): number

Sets a repeating timer for the system to repeatedly call a function at a fixed interval.

E
ester.zhou 已提交
78 79
**System capability**: SystemCapability.ArkUI.ArkUI.Full

E
ester.zhou 已提交
80
**Parameters**
81

E
ester.zhou 已提交
82 83 84 85 86
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| handler | Function | Yes| Function to be called repeatedly.|
| delay | number | No| Number of milliseconds delayed before the execution.|
| ...args | Array<any> | No| Additional parameters to pass to the handler after the timer goes off.|
87

E
ester.zhou 已提交
88
**Return value**
89

E
ester.zhou 已提交
90 91 92
| Type| Description|
| -------- | -------- |
| number | ID of the repeating timer.|
93

E
ester.zhou 已提交
94
**Example**
95

E
ester.zhou 已提交
96 97 98 99 100 101 102
  ```js
  export default {    
    setInterval() {        
      var intervalID = setInterval(function() {            
        console.log('do very 1s.');        
      }, 1000);    
    }
103
  }
E
ester.zhou 已提交
104
  ```
E
ester.zhou 已提交
105

Z
zengyawen 已提交
106

107
## clearInterval
Z
zengyawen 已提交
108

109 110 111 112
clearInterval(intervalID: number): void

Cancels the repeating timer set via **setInterval()**.

E
ester.zhou 已提交
113 114
**System capability**: SystemCapability.ArkUI.ArkUI.Full

E
ester.zhou 已提交
115
**Parameters**
116

E
ester.zhou 已提交
117 118 119
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| intervalID | number | Yes| ID of the repeating timer to cancel, which is returned by **setInterval()**.|
120

E
ester.zhou 已提交
121
**Example**
122

E
ester.zhou 已提交
123 124 125 126 127 128 129 130
  ```js
  export default {    
    clearInterval() {        
      var intervalID = setInterval(function() {
        console.log('do very 1s.');
      }, 1000);
      clearInterval(intervalID);
    }
131
  }
E
ester.zhou 已提交
132
  ```