js-apis-system-timer.md 11.7 KB
Newer Older
M
MangTsang 已提交
1
# 系统定时器
2

M
MangTsang 已提交
3
本模块主要由系统定时器功能组成。开发者可以使用定时功能实现定时服务,如闹钟等。
4 5

> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
6 7
>- 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
>- 本模块接口为系统接口,三方应用不支持调用。
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

## 导入模块


```
import systemTimer from '@ohos.systemTimer';
```


## systemTime.createTimer

createTimer(options: TimerOptions, callback: AsyncCallback<number>): void

创建定时器。

**系统能力:** SystemCapability.MiscServices.Time

**参数:**

27 28
| 名称    | 类型                             | 必填 | 说明                                                                        |
| ------- | ---------------------------------| ---- | --------------------------------------------------------------------------- |
29
| options |  [TimerOptions](#timeroptions)   | 是   | 创建系统定时器,包括定时器类型,是否循环触发,间隔时间,WantAgent通知机制。 |
30 31 32 33 34 35 36 37 38 39

**返回值:**

| 类型                      | 说明                                                         |
| ------------------------- | ------------------------------------------------------------ |
| syncCallback<number>| 以回调函数的形式返回定时器的ID。                             |

**示例:**

  ```js
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
export default {
    systemTimer () {
        var options = {
            type: systemTimer.TIMER_TYPE_REALTIME,
            repeat: false
        }
        systemTimer.createTimer(options, (error, data) => {
            if (error) {
                console.error(`failed to systemTime.createTimer ` + JSON.stringify(error));
                return;
            }
            console.log(`systemTime.createTimer success data : ` + JSON.stringify(data));
        });
    }
}
55 56 57 58 59 60 61 62 63 64 65 66 67
  ```


## systemTime.createTimer

createTimer(options: TimerOptions): Promise<number>

创建定时器。

**系统能力:** SystemCapability.MiscServices.Time

**参数:**

68 69 70
| 名称    | 类型                             | 必填 | 说明                                                                        |
| ------- | ---------------------------------| ---- | --------------------------------------------------------------------------- |
| options |  [TimerOptions](#TimerOptions)   | 是   | 创建系统定时器,包括定时器类型,是否循环触发,间隔时间,WantAgent通知机制。 |
71 72 73 74 75 76 77 78 79 80

**返回值:**

| 类型                  | 说明                                                         |
| --------------------- | ------------------------------------------------------------ |
| Promise<number> | 以Promise形式返回定时器的ID。                                |

**示例:**

  ```js
81 82 83 84 85 86 87 88 89 90 91 92 93
export default {
    systemTimer () {
        var options = {
            type: systemTimer.TIMER_TYPE_REALTIME,
            repeat:false
        }
        systemTimer.createTimer(options).then((data) => {
            console.log(`systemTime.createTimer success data : ` + JSON.stringify(data));
        }).catch((error) => {
            console.error(`failed to systemTime.createTimer because ` + JSON.stringify(error));
        });
    }
}
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  ```


## systemTime.startTimer

startTimer(timer: number, triggerTime: number, callback: AsyncCallback<void>): void

开始定时器。

**系统能力:** SystemCapability.MiscServices.Time

**参数:**

| 参数名      | 类型                        | 必填 | 说明                                                         |
| ----------- | --------------------------- | ---- | ------------------------------------------------------------ |
| timer       | number                      | 是   | 定时器的ID。                                                 |                                                                                                             
| triggerTime | number                      | 是   | 定时器的触发时间,单位:毫秒。                               |


**示例:**

  ```js
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
export default {
    systemTimer () {
        var options = {
            type: systemTimer.TIMER_TYPE_REALTIME,
            repeat:false
        }
        let timerId = systemTimer.Timer(options)
        systemTimer.startTimer(timerId, 10000, (error, data) => {
            if (error) {
                console.error(`failed to systemTime.startTimer ` + JSON.stringify(error));
                return;
            }
            console.log(`systemTime.startTimer success data : ` + JSON.stringify(data));
        });
    }
}
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  ```
  
## systemTime.startTimer

startTimer(timer: number, triggerTime: number): Promise<void>

开始定时器。

**系统能力:** SystemCapability.MiscServices.Time

**参数:**

| 参数名      | 类型                        | 必填 | 说明                                                         |
| ----------- | --------------------------- | ---- | ------------------------------------------------------------ |
| timer       | number                      | 是   | 定时器的ID。                                                                                                                                                             
| triggerTime | number                      | 是   | 定时器的触发时间,单位:毫秒。                               |                                                                                                                    


**示例:**

  ```js
153 154 155 156 157 158 159 160 161 162 163 164 165 166
export default {
    systemTimer (){
        var options = {
            type: systemTimer.TIMER_TYPE_REALTIME,
            repeat:false
        }
        let timerId = systemTimer.Timer(options)
        systemTimer.startTimer(timerId, 10000).then((data) => {
            console.log(`systemTime.startTimer success data : ` + JSON.stringify(data));
        }).catch((error) => {
            console.error(`failed to systemTime.startTimer because ` + JSON.stringify(error));
        });
    }
}
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
  ```


## systemTime.stopTimer

stopTimer(timer: number, callback: AsyncCallback<void>): void

停止定时器。

**系统能力:** SystemCapability.MiscServices.Time

**参数:**

| 参数名   | 类型                        | 必填 | 说明                                                         |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
| timer    | number                      | 是   | 定时器的ID。                                                 |

**示例:**

  ```js
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
export default {
    systemTimer () {
        var options = {
            type: systemTimer.TIMER_TYPE_REALTIME,
            repeat:false
        }
        let timerId = systemTimer.Timer(options)
        systemTimer.startTimer(timerId, 100000)
        systemTimer.stoptTimer(timerId, 10000, (error, data) => {
            if (error) {
                console.error(`failed to systemTime.startTimer ` + JSON.stringify(error));
                return;
            }
            console.log(`systemTime.startTimer success data : ` + JSON.stringify(data));
        });
    }
}
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
  ```


## systemTime.stopTimer

stopTimer(timer: number): Promise<void>

停止定时器。

**系统能力:** SystemCapability.MiscServices.Time

**参数:**

| 参数名 | 类型    | 必填 | 说明                                                         |
| ------ | ------- | ---- | ------------------------------------------------------------ |
| timer  | number  | 是   | 定时器的ID。                                                 |                                                                                                                      

**示例:**

  ```js
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
export default {
    systemTimer (){
        var options = {
            type: systemTimer.TIMER_TYPE_REALTIME,
            repeat:false
        }
        let timerId = systemTimer.Timer(options)
        systemTimer.startTimer(timerId, 100000)
        systemTimer.stoptTimer(timerId, 10000).then((data) => {
            console.log(`systemTime.startTimer success data : ` + JSON.stringify(data));
        }).catch((error) => {
            console.error(`failed to systemTime.startTimer because ` + JSON.stringify(error));
        });
    }
}
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
  ```


## systemTime.destroyTimer

destroyTimer(timer: number, callback: AsyncCallback<void>): void

销毁定时器。

**系统能力:** SystemCapability.MiscServices.Time

**参数:**

| 参数名   | 类型                        | 必填 | 说明                                                         |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
| timer    | number                      | 是   | 定时器的ID。                                                 |

**示例:**

  ```js
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
export default {
    systemTimer () {
        var options = {
            type: systemTimer.TIMER_TYPE_REALTIME,
            repeat:false
        }
        let timerId = systemTimer.Timer(options)
        systemTimer.startTimer(timerId, 100000)
        systemTimer.stopTimer(timerId)
        systemTimer.destroyTimer(timerId, (error, data) => {
            if (error) {
                console.error(`failed to systemTime.startTimer ` + JSON.stringify(error));
                return;
            }
            console.log(`systemTime.startTimer success data : ` + JSON.stringify(data));
        });
    }
}
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
  ```


## systemTime.destroyTimer

destroyTimer(timer: number): Promise<void>

销毁定时器。

**系统能力:** SystemCapability.MiscServices.Time

**参数:**

| 参数名 | 类型    | 必填 | 说明                                                         |
| ------ | ------- | ---- | ------------------------------------------------------------ |
| timer  | number  | 是   | 定时器的ID。                                                 |

**示例:**

  ```js
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
export default {
    systemTimer (){
        var options = {
            type: systemTimer.TIMER_TYPE_REALTIME,
            repeat:false
        }
        let timerId = systemTimer.Timer(options)
        systemTimer.startTimer(timerId, 100000)
        systemTimer.stopTimer(timerId)
        systemTimer.destroytTimer(timerId, 10000).then((data) => {
            console.log(`systemTime.startTimer success data : ` + JSON.stringify(data));
        }).catch((error) => {
            console.error(`failed to systemTime.startTimer because ` + JSON.stringify(error));
        });
    }
}
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
  ```
  
 ## TimerOptions

createTimer的初始化选项。

**系统能力:** SystemCapability.MiscServices.Time

| 参数名   | 类型              | 必填 | 说明                                                                                            |
| -------- | ------------------| ---- | ----------------------------------------------------------------------------------------------- |
| type     | number            | 是   | const TIMER_TYPE_REALTIME: 设置为系统启动时间定时器,否则为walltime定时器; <br/>const TIMER_TYPE_WAKEUP: 设置为唤醒定时器,否则为非唤醒; <br/>const TIMER_TYPE_EXACT: 设置为精准定时器,否则为非精准定时器; <br/>const TIMER_TYPE_IDLE: number: 设置为IDLE模式定时器,否则为非IDLE模式定时器(暂不支持) |
| repeat   | boolean           | 是   | true 为循环定时器,false为单次定时器。                                                          |
| interval | number            | 否   | 如果是循环定时器,repeat值应大于5000毫秒,非重复定时器置为0。                                   |
| wantAgent| wantAgent         | 否   | 设置通知的wantagent,定时器到期后通知。(支持拉起应用MainAbility,暂不支持拉起ServiceAbility)     |