提交 b53b4123 编写于 作者: E ester.zhou

Update docs (13058)

Signed-off-by: Nester.zhou <ester.zhou@huawei.com>
上级 05b03302
# System Timer # @ohos.systemTimer
The **systemTimer** module provides system timer features. You can use the APIs of this module to implement the alarm clock and other timer services. The **systemTimer** module provides system timer features. You can use the APIs of this module to implement the alarm clock and other timer services.
...@@ -33,13 +33,13 @@ Defines the initialization options for **createTimer**. ...@@ -33,13 +33,13 @@ Defines the initialization options for **createTimer**.
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
| Name | Type | Mandatory| Description | | 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>**5**: idle type (not supported currently).| | 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. | | 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**.| | 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-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.)| | 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. | | callback | number | Yes | Callback used to return the timer ID. |
## systemTimer.createTimer ## systemTimer.createTimer
...@@ -48,6 +48,8 @@ createTimer(options: TimerOptions, callback: AsyncCallback&lt;number&gt;): void ...@@ -48,6 +48,8 @@ createTimer(options: TimerOptions, callback: AsyncCallback&lt;number&gt;): void
Creates a timer. This API uses an asynchronous callback to return the result. Creates a timer. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -66,13 +68,17 @@ export default { ...@@ -66,13 +68,17 @@ export default {
type: systemTimer.TIMER_TYPE_REALTIME, type: systemTimer.TIMER_TYPE_REALTIME,
repeat: false repeat: false
}; };
systemTimer.createTimer(options, (error, data) => { try {
if (error) { systemTimer.createTimer(options, (error) => {
console.error(`Failed to create timer. Cause:` + JSON.stringify(error)); if (error) {
return; console.info(`Failed to create timer. message:${error.message}, code:${error.code}`);
} return;
console.log(`Succeeded in creating timer. Data:` + JSON.stringify(data)); }
}); console.info(`Succeeded in creating timer.`);
});
} catch(e) {
console.info(`Failed to create timer. message:${e.message}, code:${e.code}`);
}
} }
} }
``` ```
...@@ -83,6 +89,8 @@ createTimer(options: TimerOptions): Promise&lt;number&gt; ...@@ -83,6 +89,8 @@ createTimer(options: TimerOptions): Promise&lt;number&gt;
Creates a timer. This API uses a promise to return the result. Creates a timer. This API uses a promise to return the result.
**System API**: This is a system API.
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -105,12 +113,16 @@ export default { ...@@ -105,12 +113,16 @@ export default {
let options = { let options = {
type: systemTimer.TIMER_TYPE_REALTIME, type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false repeat:false
}; };
systemTimer.createTimer(options).then((data) => { try {
console.log(`Succeeded in creating timer. Data:` + JSON.stringify(data)); systemTimer.createTimer(options).then(() => {
}).catch((error) => { console.info(`Succeeded in creating timer.`);
console.error(`Failed to create timer. Cause:` + JSON.stringify(error)); }).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}`);
}
} }
} }
``` ```
...@@ -121,6 +133,8 @@ startTimer(timer: number, triggerTime: number, callback: AsyncCallback&lt;void&g ...@@ -121,6 +133,8 @@ startTimer(timer: number, triggerTime: number, callback: AsyncCallback&lt;void&g
Starts a timer. This API uses an asynchronous callback to return the result. Starts a timer. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -143,12 +157,17 @@ export default { ...@@ -143,12 +157,17 @@ export default {
let timerId = await systemTimer.createTimer(options) let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime() let triggerTime = new Date().getTime()
triggerTime += 3000 triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime, (error) => { try {
if (error) { systemTimer.startTimer(timerId, triggerTime, (error) => {
console.error(`Failed to start timer. Cause:` + JSON.stringify(error)); if (error) {
return; 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}`);
}
} }
} }
``` ```
...@@ -159,6 +178,8 @@ startTimer(timer: number, triggerTime: number): Promise&lt;void&gt; ...@@ -159,6 +178,8 @@ startTimer(timer: number, triggerTime: number): Promise&lt;void&gt;
Starts a timer. This API uses a promise to return the result. Starts a timer. This API uses a promise to return the result.
**System API**: This is a system API.
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -186,11 +207,15 @@ export default { ...@@ -186,11 +207,15 @@ export default {
let timerId = await systemTimer.createTimer(options) let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime() let triggerTime = new Date().getTime()
triggerTime += 3000 triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime).then((data) => { try {
console.log(`Succeeded in startting timer. Data:` + JSON.stringify(data)); systemTimer.startTimer(timerId, triggerTime).then(() => {
}).catch((error) => { console.info(`Succeeded in starting timer.`);
console.error(`Failed to start timer. Cause:` + JSON.stringify(error)); }).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}`);
}
} }
} }
``` ```
...@@ -201,6 +226,8 @@ stopTimer(timer: number, callback: AsyncCallback&lt;void&gt;): void ...@@ -201,6 +226,8 @@ stopTimer(timer: number, callback: AsyncCallback&lt;void&gt;): void
Stops a timer. This API uses an asynchronous callback to return the result. Stops a timer. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -214,22 +241,27 @@ Stops a timer. This API uses an asynchronous callback to return the result. ...@@ -214,22 +241,27 @@ Stops a timer. This API uses an asynchronous callback to return the result.
```js ```js
export default { export default {
async systemTimer () { async systemTimer () {
let options = { let options = {
type: systemTimer.TIMER_TYPE_REALTIME, type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false repeat:false
} }
let timerId = await systemTimer.createTimer(options) let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime() let triggerTime = new Date().getTime()
triggerTime += 3000 triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime) systemTimer.startTimer(timerId, triggerTime)
systemTimer.stopTimer(timerId, (error) => { try {
if (error) { systemTimer.stopTimer(timerId, (error) => {
console.error(`Failed to stop timer. Cause:` + JSON.stringify(error)); if (error) {
return; 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}`);
}
}
} }
``` ```
...@@ -239,6 +271,8 @@ stopTimer(timer: number): Promise&lt;void&gt; ...@@ -239,6 +271,8 @@ stopTimer(timer: number): Promise&lt;void&gt;
Stops a timer. This API uses a promise to return the result. Stops a timer. This API uses a promise to return the result.
**System API**: This is a system API.
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -257,21 +291,25 @@ Stops a timer. This API uses a promise to return the result. ...@@ -257,21 +291,25 @@ Stops a timer. This API uses a promise to return the result.
```js ```js
export default { export default {
async systemTimer (){ async systemTimer (){
let options = { let options = {
type: systemTimer.TIMER_TYPE_REALTIME, type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false repeat:false
} }
let timerId = await systemTimer.createTimer(options) let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime() let triggerTime = new Date().getTime()
triggerTime += 3000 triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime) systemTimer.startTimer(timerId, triggerTime)
systemTimer.stopTimer(timerId).then((data) => { try {
console.log(`Succeeded in stopping timer. Data:` + JSON.stringify(data)); systemTimer.stopTimer(timerId).then(() => {
}).catch((error) => { console.info(`Succeeded in stopping timer.`);
console.error(`Failed to stop timer. Cause:` + JSON.stringify(error)); }).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}`);
}
}
} }
``` ```
...@@ -281,6 +319,8 @@ destroyTimer(timer: number, callback: AsyncCallback&lt;void&gt;): void ...@@ -281,6 +319,8 @@ destroyTimer(timer: number, callback: AsyncCallback&lt;void&gt;): void
Destroys a timer. This API uses an asynchronous callback to return the result. Destroys a timer. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -304,12 +344,17 @@ export default { ...@@ -304,12 +344,17 @@ export default {
triggerTime += 3000 triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime) systemTimer.startTimer(timerId, triggerTime)
systemTimer.stopTimer(timerId) systemTimer.stopTimer(timerId)
systemTimer.destroyTimer(timerId, (error) => { try {
if (error) { systemTimer.destroyTimer(timerId, (error) => {
console.error(`Failed to destroy timer. Cause:` + JSON.stringify(error)); if (error) {
return; 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}`);
}
} }
} }
``` ```
...@@ -320,6 +365,8 @@ destroyTimer(timer: number): Promise&lt;void&gt; ...@@ -320,6 +365,8 @@ destroyTimer(timer: number): Promise&lt;void&gt;
Destroys a timer. This API uses a promise to return the result. Destroys a timer. This API uses a promise to return the result.
**System API**: This is a system API.
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -348,11 +395,15 @@ export default { ...@@ -348,11 +395,15 @@ export default {
triggerTime += 3000 triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime) systemTimer.startTimer(timerId, triggerTime)
systemTimer.stopTimer(timerId) systemTimer.stopTimer(timerId)
systemTimer.destroyTimer(timerId).then((data) => { try {
console.log(`Succeeded in destroying timer. Data:` + JSON.stringify(data)); systemTimer.destroyTimer(timerId).then(() => {
}).catch((error) => { console.info(`Succeeded in destroying timer.`);
console.error(`Failed to destroy timer. Cause:` + JSON.stringify(error)); }).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}`);
}
} }
} }
``` ```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册