js-apis-system-timer.md 13.7 KB
Newer Older
E
ester.zhou 已提交
1
# @ohos.systemTimer
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 66
export default {
    systemTimer () {
E
ester.zhou 已提交
67
        let options = {
E
ester.zhou 已提交
68 69
            type: systemTimer.TIMER_TYPE_REALTIME,
            repeat: false
E
ester.zhou 已提交
70
        };
E
ester.zhou 已提交
71 72 73 74 75 76 77 78 79 80 81
        try {
            systemTimer.createTimer(options, (error) => {
                if (error) {
                    console.info(`Failed to create timer. message:${error.message}, code:${error.code}`);
                    return;
                }
           		console.info(`Succeeded in creating timer.`);
        	});
        } catch(e) {
            console.info(`Failed to create timer. message:${e.message}, code:${e.code}`);
        }
E
ester.zhou 已提交
82 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 112
export default {
    systemTimer () {
E
ester.zhou 已提交
113
        let options = {
E
ester.zhou 已提交
114 115
            type: systemTimer.TIMER_TYPE_REALTIME,
            repeat:false
E
ester.zhou 已提交
116 117 118 119 120 121 122 123 124 125
        };     
        try {
    		systemTimer.createTimer(options).then(() => {
    			console.info(`Succeeded in creating timer.`);
			}).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 已提交
126 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
    async systemTimer () {
        let options = {
E
ester.zhou 已提交
154 155 156
            type: systemTimer.TIMER_TYPE_REALTIME,
            repeat:false
        }
E
ester.zhou 已提交
157 158 159
      let timerId = await systemTimer.createTimer(options)
      let triggerTime = new Date().getTime()
      triggerTime += 3000
E
ester.zhou 已提交
160 161 162 163 164 165 166 167 168 169 170
      try {
          systemTimer.startTimer(timerId, triggerTime, (error) => {
              if (error) {
                  console.info(`Failed to start timer. message:${error.message}, code:${error.code}`);
                  return;
              }
              console.info(`Succeeded in starting timer.`);
          });
      } catch(e) {
          console.info(`Failed to start timer. message:${e.message}, code:${e.code}`);
      }
E
ester.zhou 已提交
171 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
    async systemTimer (){
        let options = {
E
ester.zhou 已提交
204 205 206
            type: systemTimer.TIMER_TYPE_REALTIME,
            repeat:false
        }
E
ester.zhou 已提交
207 208
        let timerId = await systemTimer.createTimer(options)
        let triggerTime = new Date().getTime()
E
ester.zhou 已提交
209
        triggerTime += 3000
E
ester.zhou 已提交
210 211 212 213 214 215 216 217 218
        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 已提交
219 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 258 259 260 261 262 263 264
    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;
                }
                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 311 312
    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 已提交
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
    async systemTimer () {
        let options = {
E
ester.zhou 已提交
339 340 341
            type: systemTimer.TIMER_TYPE_REALTIME,
            repeat:false
        }
E
ester.zhou 已提交
342 343
        let timerId = await systemTimer.createTimer(options)
        let triggerTime = new Date().getTime()
E
ester.zhou 已提交
344 345
        triggerTime += 3000
        systemTimer.startTimer(timerId, triggerTime)
E
ester.zhou 已提交
346
        systemTimer.stopTimer(timerId)
E
ester.zhou 已提交
347 348 349 350 351 352 353 354 355 356 357
        try {
            systemTimer.destroyTimer(timerId, (error) => {
                if (error) {
                    console.info(`Failed to destroy timer. message:${error.message}, code:${error.code}`);
                    return;
                }
                console.info(`Succeeded in destroying timer.`);
        	});
        } catch(e) {
            console.info(`Failed to destroying timer. message:${e.message}, code:${e.code}`);
        }
E
ester.zhou 已提交
358 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
    async systemTimer (){
        let options = {
E
ester.zhou 已提交
390 391 392
            type: systemTimer.TIMER_TYPE_REALTIME,
            repeat:false
        }
E
ester.zhou 已提交
393 394
        let timerId = await systemTimer.createTimer(options)
        let triggerTime = new Date().getTime()
E
ester.zhou 已提交
395 396
        triggerTime += 3000
        systemTimer.startTimer(timerId, triggerTime)
E
ester.zhou 已提交
397
        systemTimer.stopTimer(timerId)
E
ester.zhou 已提交
398 399 400 401 402 403 404 405 406
        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 已提交
407 408
    }
}
E
ester.zhou 已提交
409
```