js-apis-system-timer.md 13.1 KB
Newer Older
E
ester.zhou 已提交
1
# @ohos.systemTimer (System Timer)
E
ester.zhou 已提交
2 3 4

The **systemTimer** module provides system timer features. You can use the APIs of this module to implement the alarm clock and other timer services.

E
ester.zhou 已提交
5
> **NOTE**
E
ester.zhou 已提交
6 7 8
>
> - The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> - The APIs provided by this module are system APIs.
E
ester.zhou 已提交
9 10 11 12

## Modules to Import


E
ester.zhou 已提交
13
```js
E
ester.zhou 已提交
14 15 16
import systemTimer from '@ohos.systemTimer';
```

E
ester.zhou 已提交
17 18 19 20 21 22 23 24 25 26 27 28
## Constants

Provides the constants that define the supported timer types.

**System capability**: SystemCapability.MiscServices.Time

| Name               | Type  | Value  | Description                        |
| ------------------- | ------ | ---- | ---------------------------- |
| TIMER_TYPE_REALTIME | number | 1    | CPU time type. (The start time of the timer cannot be later than the current system time.)        |
| TIMER_TYPE_WAKEUP   | number | 2    | Wakeup type.                |
| TIMER_TYPE_EXACT    | number | 4    | Exact type.                |
| TIMER_TYPE_IDLE     | number | 8    | Idle type (not supported currently).|
E
ester.zhou 已提交
29

E
ester.zhou 已提交
30 31 32 33 34 35
 ## TimerOptions

Defines the initialization options for **createTimer**.

**System capability**: SystemCapability.MiscServices.Time

E
ester.zhou 已提交
36 37 38 39 40 41 42
| Name     | Type                                         | Mandatory| Description                                                        |
| --------- | --------------------------------------------- | ---- | ------------------------------------------------------------ |
| type      | number                                        | Yes  | Timer type.<br>**1**: CPU time type. (The start time of the timer cannot be later than the current system time.)<br>**2**: wakeup type.<br>**4**: exact type.<br>**8**: idle type (not supported currently).|
| repeat    | boolean                                       | Yes  | Whether the timer is a repeating timer. The value **true** means that the timer is a repeating timer, and **false** means that the timer is a one-shot timer.                       |
| interval  | number                                        | No  | Repeat interval. For a repeating timer, the value must be greater than 5000 ms. For a one-shot timer, the value is **0**.|
| wantAgent | [WantAgent](js-apis-app-ability-wantAgent.md) | No  | **WantAgent** object of the notification to be sent when the timer expires. (An application MainAbility can be started, but not a Service ability.)|
| callback  | number                                        | Yes  | Callback used to return the timer ID.                            |
E
ester.zhou 已提交
43 44 45


## systemTimer.createTimer
E
ester.zhou 已提交
46 47 48 49 50

createTimer(options: TimerOptions, callback: AsyncCallback&lt;number&gt;): void

Creates a timer. This API uses an asynchronous callback to return the result.

E
ester.zhou 已提交
51 52
**System API**: This is a system API.

E
ester.zhou 已提交
53 54 55 56
**System capability**: SystemCapability.MiscServices.Time

**Parameters**

E
ester.zhou 已提交
57 58 59 60
| Name  | Type                         | Mandatory| Description                                                        |
| -------- | ----------------------------- | ---- | ------------------------------------------------------------ |
| options  | [TimerOptions](#timeroptions) | Yes  | Timer initialization options, including the timer type, whether the timer is a repeating timer, interval, and **WantAgent** options.|
| callback | AsyncCallback&lt;number>      | Yes  | Callback used to return the timer ID.                                  |
E
ester.zhou 已提交
61 62 63

**Example**

E
ester.zhou 已提交
64
```js
E
ester.zhou 已提交
65
export default {
E
ester.zhou 已提交
66 67 68 69 70 71 72 73 74 75
  systemTimer () {
    let options = {
      type: systemTimer.TIMER_TYPE_REALTIME,
      repeat: false
    };
    try {
      systemTimer.createTimer(options, (error, timerId) => {
        if (error) {
          console.info(`Failed to create timer. message: ${error.message}, code: ${error.code}`);
          return;
E
ester.zhou 已提交
76
        }
E
ester.zhou 已提交
77 78 79 80
        console.info(`Succeeded in creating timer. timerId: ${timerId}`);
      });
    } catch(e) {
      console.info(`Failed to create timer. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
81
    }
E
ester.zhou 已提交
82
  }
E
ester.zhou 已提交
83
}
E
ester.zhou 已提交
84
```
E
ester.zhou 已提交
85

E
ester.zhou 已提交
86
## systemTimer.createTimer
E
ester.zhou 已提交
87 88 89 90 91

createTimer(options: TimerOptions): Promise&lt;number&gt;

Creates a timer. This API uses a promise to return the result.

E
ester.zhou 已提交
92 93
**System API**: This is a system API.

E
ester.zhou 已提交
94 95 96 97
**System capability**: SystemCapability.MiscServices.Time

**Parameters**

E
ester.zhou 已提交
98 99 100
| Name | Type                         | Mandatory| Description                                                        |
| ------- | ----------------------------- | ---- | ------------------------------------------------------------ |
| options | [TimerOptions](#timeroptions) | Yes  | Timer initialization options, including the timer type, whether the timer is a repeating timer, interval, and **WantAgent** options.|
E
ester.zhou 已提交
101 102 103

**Return value**

E
ester.zhou 已提交
104 105 106
| Type                 | Description                         |
| --------------------- | ----------------------------- |
| Promise&lt;number&gt; | Promise used to return the timer ID.|
E
ester.zhou 已提交
107 108 109

**Example**

E
ester.zhou 已提交
110
```js
E
ester.zhou 已提交
111
export default {
E
ester.zhou 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124
  systemTimer () {
    let options = {
      type: systemTimer.TIMER_TYPE_REALTIME,
      repeat:false
    };   
    try {
      systemTimer.createTimer(options).then((timerId) => {
        console.info(`Succeeded in creating timer. timerId: ${timerId}`);
      }).catch((error) => {
        console.info(`Failed to create timer. message: ${error.message}, code: ${error.code}`);
      });
    } catch(e) {
      console.info(`Failed to create timer. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
125
    }
E
ester.zhou 已提交
126
  }
E
ester.zhou 已提交
127
}
E
ester.zhou 已提交
128
```
E
ester.zhou 已提交
129

E
ester.zhou 已提交
130
## systemTimer.startTimer
E
ester.zhou 已提交
131 132 133 134 135

startTimer(timer: number, triggerTime: number, callback: AsyncCallback&lt;void&gt;): void

Starts a timer. This API uses an asynchronous callback to return the result.

E
ester.zhou 已提交
136 137
**System API**: This is a system API.

E
ester.zhou 已提交
138 139 140 141
**System capability**: SystemCapability.MiscServices.Time

**Parameters**

E
ester.zhou 已提交
142 143 144 145 146
| Name     | Type                  | Mandatory| Description                          |
| ----------- | ---------------------- | ---- | ------------------------------ |
| timer       | number                 | Yes  | ID of the timer.                  |
| triggerTime | number                 | Yes  | Time when the timer is triggered, in milliseconds.|
| callback    | AsyncCallback&lt;void> | Yes  | Callback used to return the result.                    |
E
ester.zhou 已提交
147 148 149

**Example**

E
ester.zhou 已提交
150
```js
E
ester.zhou 已提交
151
export default {
E
ester.zhou 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164
  async systemTimer () {
    let options = {
      type: systemTimer.TIMER_TYPE_REALTIME,
      repeat:false
    }
  let timerId = await systemTimer.createTimer(options);
  let triggerTime = new Date().getTime();
  triggerTime += 3000;
  try {
      systemTimer.startTimer(timerId, triggerTime, (error) => {
        if (error) {
          console.info(`Failed to start timer. message: ${error.message}, code: ${error.code}`);
          return;
E
ester.zhou 已提交
165
        }
E
ester.zhou 已提交
166 167 168 169
        console.info(`Succeeded in starting timer.`);
      });
    } catch(e) {
      console.info(`Failed to start timer. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
170
    }
E
ester.zhou 已提交
171
  }
E
ester.zhou 已提交
172
}
E
ester.zhou 已提交
173
```
E
ester.zhou 已提交
174

E
ester.zhou 已提交
175
## systemTimer.startTimer
E
ester.zhou 已提交
176 177 178 179 180

startTimer(timer: number, triggerTime: number): Promise&lt;void&gt;

Starts a timer. This API uses a promise to return the result.

E
ester.zhou 已提交
181 182
**System API**: This is a system API.

E
ester.zhou 已提交
183 184 185 186
**System capability**: SystemCapability.MiscServices.Time

**Parameters**

E
ester.zhou 已提交
187 188 189 190 191 192 193 194 195 196
| Name     | Type  | Mandatory| Description                          |
| ----------- | ------ | ---- | ------------------------------ |
| timer       | number | Yes  | ID of the timer.                  |
| triggerTime | number | Yes  | Time when the timer is triggered, in milliseconds.|

**Return value**

| Type          | Description                     |
| -------------- | ------------------------- |
| Promise\<void> | Promise that returns no value.|
E
ester.zhou 已提交
197 198 199

**Example**

E
ester.zhou 已提交
200
```js
E
ester.zhou 已提交
201
export default {
E
ester.zhou 已提交
202 203 204 205
  async systemTimer (){
    let options = {
      type: systemTimer.TIMER_TYPE_REALTIME,
      repeat:false
E
ester.zhou 已提交
206
    }
E
ester.zhou 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219
    let timerId = await systemTimer.createTimer(options);
    let triggerTime = new Date().getTime();
    triggerTime += 3000;
    try {
      systemTimer.startTimer(timerId, triggerTime).then(() => {
        console.info(`Succeeded in starting timer.`);
         }).catch((error) => {
        console.info(`Failed to start timer. message: ${error.message}, code: ${error.code}`);
      });
    } catch(e) {
      console.info(`Failed to start timer. message: ${e.message}, code: ${e.code}`);
    } 
  }
E
ester.zhou 已提交
220
}
E
ester.zhou 已提交
221
```
E
ester.zhou 已提交
222

E
ester.zhou 已提交
223
## systemTimer.stopTimer
E
ester.zhou 已提交
224 225 226 227 228

stopTimer(timer: number, callback: AsyncCallback&lt;void&gt;): void

Stops a timer. This API uses an asynchronous callback to return the result.

E
ester.zhou 已提交
229 230
**System API**: This is a system API.

E
ester.zhou 已提交
231 232 233 234
**System capability**: SystemCapability.MiscServices.Time

**Parameters**

E
ester.zhou 已提交
235 236 237 238
| Name  | Type                  | Mandatory| Description        |
| -------- | ---------------------- | ---- | ------------ |
| timer    | number                 | Yes  | ID of the timer.|
| callback | AsyncCallback&lt;void> | Yes  | Callback used to return the result.  |
E
ester.zhou 已提交
239 240 241

**Example**

E
ester.zhou 已提交
242
```js
E
ester.zhou 已提交
243
export default {
E
ester.zhou 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257
  async systemTimer () {
    let options = {
      type: systemTimer.TIMER_TYPE_REALTIME,
      repeat:false
    }
    let timerId = await systemTimer.createTimer(options);
    let triggerTime = new Date().getTime();
    triggerTime += 3000;
    systemTimer.startTimer(timerId, triggerTime);
    try {
      systemTimer.stopTimer(timerId, (error) => {
        if (error) {
          console.info(`Failed to stop timer. message: ${error.message}, code: ${error.code}`);
          return;
E
ester.zhou 已提交
258
        }
E
ester.zhou 已提交
259 260 261 262 263 264
        console.info(`Succeeded in stopping timer.`);
      });
    } catch(e) {
      console.info(`Failed to stop timer. message: ${e.message}, code: ${e.code}`);
    }
  }
E
ester.zhou 已提交
265
}
E
ester.zhou 已提交
266
```
E
ester.zhou 已提交
267

E
ester.zhou 已提交
268
## systemTimer.stopTimer
E
ester.zhou 已提交
269 270 271 272 273

stopTimer(timer: number): Promise&lt;void&gt;

Stops a timer. This API uses a promise to return the result.

E
ester.zhou 已提交
274 275
**System API**: This is a system API.

E
ester.zhou 已提交
276 277 278 279
**System capability**: SystemCapability.MiscServices.Time

**Parameters**

E
ester.zhou 已提交
280 281 282 283 284 285 286 287 288
| Name| Type  | Mandatory| Description        |
| ------ | ------ | ---- | ------------ |
| timer  | number | Yes  | ID of the timer.|

**Return value**

| Type          | Description                     |
| -------------- | ------------------------- |
| Promise\<void> | Promise that returns no value.|
E
ester.zhou 已提交
289 290 291

**Example**

E
ester.zhou 已提交
292
```js
E
ester.zhou 已提交
293
export default {
E
ester.zhou 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
  async systemTimer (){
    let options = {
      type: systemTimer.TIMER_TYPE_REALTIME,
      repeat:false
    }
    let timerId = await systemTimer.createTimer(options);
    let triggerTime = new Date().getTime();
    triggerTime += 3000;
    systemTimer.startTimer(timerId, triggerTime);
    try {
      systemTimer.stopTimer(timerId).then(() => {
        console.info(`Succeeded in stopping timer.`);
      }).catch((error) => {
        console.info(`Failed to stop timer. message: ${error.message}, code: ${error.code}`);
      });
    } catch(e) {
      console.info(`Failed to stop timer. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
311
    }
E
ester.zhou 已提交
312
  }
E
ester.zhou 已提交
313
}
E
ester.zhou 已提交
314
```
E
ester.zhou 已提交
315

E
ester.zhou 已提交
316
## systemTimer.destroyTimer
E
ester.zhou 已提交
317 318 319 320 321

destroyTimer(timer: number, callback: AsyncCallback&lt;void&gt;): void

Destroys a timer. This API uses an asynchronous callback to return the result.

E
ester.zhou 已提交
322 323
**System API**: This is a system API.

E
ester.zhou 已提交
324 325 326 327
**System capability**: SystemCapability.MiscServices.Time

**Parameters**

E
ester.zhou 已提交
328 329 330 331
| Name  | Type                  | Mandatory| Description        |
| -------- | ---------------------- | ---- | ------------ |
| timer    | number                 | Yes  | ID of the timer.|
| callback | AsyncCallback&lt;void> | Yes  | Callback used to return the result.  |
E
ester.zhou 已提交
332 333 334

**Example**

E
ester.zhou 已提交
335
```js
E
ester.zhou 已提交
336
export default {
E
ester.zhou 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
  async systemTimer () {
    let options = {
      type: systemTimer.TIMER_TYPE_REALTIME,
      repeat:false
    }
    let timerId = await systemTimer.createTimer(options);
    let triggerTime = new Date().getTime();
    triggerTime += 3000;
    systemTimer.startTimer(timerId, triggerTime);
    systemTimer.stopTimer(timerId);
    try {
      systemTimer.destroyTimer(timerId, (error) => {
        if (error) {
          console.info(`Failed to destroy timer. message: ${error.message}, code: ${error.code}`);
          return;
E
ester.zhou 已提交
352
        }
E
ester.zhou 已提交
353 354 355 356
        console.info(`Succeeded in destroying timer.`);
      });
    } catch(e) {
      console.info(`Failed to destroying timer. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
357
    }
E
ester.zhou 已提交
358
  }
E
ester.zhou 已提交
359
}
E
ester.zhou 已提交
360
```
E
ester.zhou 已提交
361

E
ester.zhou 已提交
362
## systemTimer.destroyTimer
E
ester.zhou 已提交
363 364 365 366 367

destroyTimer(timer: number): Promise&lt;void&gt;

Destroys a timer. This API uses a promise to return the result.

E
ester.zhou 已提交
368 369
**System API**: This is a system API.

E
ester.zhou 已提交
370 371 372 373
**System capability**: SystemCapability.MiscServices.Time

**Parameters**

E
ester.zhou 已提交
374 375 376 377 378 379 380 381 382
| Name| Type  | Mandatory| Description        |
| ------ | ------ | ---- | ------------ |
| timer  | number | Yes  | ID of the timer.|

**Return value**

| Type          | Description                     |
| -------------- | ------------------------- |
| Promise\<void> | Promise that returns no value.|
E
ester.zhou 已提交
383 384 385

**Example**

E
ester.zhou 已提交
386
```js
E
ester.zhou 已提交
387
export default {
E
ester.zhou 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
  async systemTimer (){
    let options = {
      type: systemTimer.TIMER_TYPE_REALTIME,
      repeat:false
    }
    let timerId = await systemTimer.createTimer(options);
    let triggerTime = new Date().getTime();
    triggerTime += 3000;
    systemTimer.startTimer(timerId, triggerTime);
    systemTimer.stopTimer(timerId);
    try {
      systemTimer.destroyTimer(timerId).then(() => {
         console.info(`Succeeded in destroying timer.`);
      }).catch((error) => {
        console.info(`Failed to destroy timer. message: ${error.message}, code: ${error.code}`);
      });
    } catch(e) {
      console.info(`Failed to destroying timer. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
406
    }
E
ester.zhou 已提交
407
  }
E
ester.zhou 已提交
408
}
E
ester.zhou 已提交
409
```