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


E
ester.zhou 已提交
4
## setTimeout
Z
zengyawen 已提交
5

E
ester.zhou 已提交
6
setTimeout(handler[,delay[,…args]]): number
Z
zengyawen 已提交
7 8 9

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

E
ester.zhou 已提交
10
**Parameters**
11

E
ester.zhou 已提交
12 13 14 15 16
| 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.|
17

E
ester.zhou 已提交
18
**Return value**
19

E
ester.zhou 已提交
20 21 22
| Type| Description|
| -------- | -------- |
| number | Timer ID.|
23

E
ester.zhou 已提交
24
**Example**
25

E
ester.zhou 已提交
26 27 28 29 30 31
```js
export default {    
  setTimeOut() {        
    var timeoutID = setTimeout(function() {            
      console.log('delay 1s');
    }, 1000);    
32
  }
E
ester.zhou 已提交
33 34 35
}
```

Z
zengyawen 已提交
36

37
## clearTimeout
Z
zengyawen 已提交
38

39
clearTimeout(timeoutID: number): void
Z
zengyawen 已提交
40

41
Cancels the timer created via **setTimeout()**.
Z
zengyawen 已提交
42

E
ester.zhou 已提交
43
**Parameters**
44

E
ester.zhou 已提交
45 46 47
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| timeoutID | number | Yes| ID of the timer to cancel, which is returned by **setTimeout()**|
48

E
ester.zhou 已提交
49
**Example**
50

E
ester.zhou 已提交
51 52 53 54 55 56 57
```js
export default {    
  clearTimeOut() {        
    var timeoutID = setTimeout(function() {            
      console.log('do after 1s delay.');        
    }, 1000);        
    clearTimeout(timeoutID);    
58
  }
E
ester.zhou 已提交
59 60 61
}
```

62 63 64 65 66 67 68

## 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 已提交
69
**Parameters**
70

E
ester.zhou 已提交
71 72 73 74 75
| 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.|
76

E
ester.zhou 已提交
77
**Return value**
78

E
ester.zhou 已提交
79 80 81
| Type| Description|
| -------- | -------- |
| number | ID of the repeating timer.|
82

E
ester.zhou 已提交
83
**Example**
84

E
ester.zhou 已提交
85 86 87 88 89 90
```js
export default {    
  setInterval() {        
    var intervalID = setInterval(function() {            
      console.log('do very 1s.');        
    }, 1000);    
91
  }
E
ester.zhou 已提交
92 93 94
}
```

Z
zengyawen 已提交
95

96
## clearInterval
Z
zengyawen 已提交
97

98 99 100 101
clearInterval(intervalID: number): void

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

E
ester.zhou 已提交
102
**Parameters**
103

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

E
ester.zhou 已提交
108
**Example**
109

E
ester.zhou 已提交
110 111 112 113 114 115 116
```js
export default {    
  clearInterval() {        
    var intervalID = setInterval(function() {
      console.log('do very 1s.');
    }, 1000);
    clearInterval(intervalID);
117
  }
E
ester.zhou 已提交
118 119
}
```