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

3
> **NOTE:** The initial APIs of this module are supported since API version 4. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
4

5
## Module to Import
Z
zengyawen 已提交
6 7 8

None

9
## Required Permissions
Z
zengyawen 已提交
10 11 12

None

13
## setTimeout
Z
zengyawen 已提交
14

15
setTimeout(handler[,delay[, ...args]]): number
Z
zengyawen 已提交
16 17 18

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

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
- Parameters

  

  | 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 parameter to pass to the handler after the timer goes off. |

- Return Value

  

  | Type   | Description |
  | ------ | ----------- |
  | number | Timer ID.   |

- Example

  ```
  export default {    
    setTimeOut() {        
      var timeoutID = setTimeout(function() {            
        console.log('delay 1s');
      }, 1000);    
Z
zengyawen 已提交
45
    }
46 47
  }
  ```
Z
zengyawen 已提交
48

49
## clearTimeout
Z
zengyawen 已提交
50

51
clearTimeout(timeoutID: number): void
Z
zengyawen 已提交
52

53
Cancels the timer created via **setTimeout()**.
Z
zengyawen 已提交
54

55 56 57 58 59 60 61 62 63
- Parameter

  

  | Name      | Type   | Mandatory | Description                                                  |
  | --------- | ------ | --------- | ------------------------------------------------------------ |
  | timeoutID | number | Yes       | ID of the timer to cancel, which is returned by **setTimeout()** |

- Example
Z
zengyawen 已提交
64

65 66 67 68 69 70 71
  ```
  export default {    
    clearTimeOut() {        
      var timeoutID = setTimeout(function() {            
        console.log('do after 1s delay.');        
      }, 1000);        
      clearTimeout(timeoutID);    
Z
zengyawen 已提交
72
    }
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  }
  ```

## setInterval

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

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

- Parameters

  

  | 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 parameter to pass to the handler after the timer goes off |

- Return Value

  

  | Type   | Description               |
  | ------ | ------------------------- |
  | number | ID of the repeated timer. |

- Example

  ```
  export default {    
    setInterval() {        
      var intervalID = setInterval(function() {            
        console.log('do very 1s.');        
      }, 1000);    
Z
zengyawen 已提交
108
    }
109 110
  }
  ```
Z
zengyawen 已提交
111

112
## clearInterval
Z
zengyawen 已提交
113

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
clearInterval(intervalID: number): void

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

- Parameter

  

  | Name       | Type   | Mandatory | Description                                                  |
  | ---------- | ------ | --------- | ------------------------------------------------------------ |
  | intervalID | number | Yes       | ID of the repeating timer to cancel, which is returned by **setInterval()**. |

- Example

  ```
  export default {    
    clearInterval() {        
      var intervalID = setInterval(function() {
        console.log('do very 1s.');
      }, 1000);
      clearInterval(intervalID);
    }
  }
  ```