1e41f4b71Sopenharmony_ci# @ohos.systemTimer (System Timer) (System API)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThe **systemTimer** module provides system timer features. You can use the APIs of this module to implement the alarm clock and other timer services.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **NOTE**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> - 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.
8e41f4b71Sopenharmony_ci> - The APIs provided by this module are system APIs.
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci## Modules to Import
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci```ts
14e41f4b71Sopenharmony_ciimport { systemTimer } from '@kit.BasicServicesKit';
15e41f4b71Sopenharmony_ci```
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci## Constants
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ciProvides the constants that define the supported timer types.
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci**System capability**: SystemCapability.MiscServices.Time
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci| Name               | Type  | Value  | Description                            |
24e41f4b71Sopenharmony_ci| ------------------- | ------ | ---- |--------------------------------|
25e41f4b71Sopenharmony_ci| TIMER_TYPE_REALTIME | number | 1    | CPU time type. (The start time of the timer cannot be later than the current system time.)|
26e41f4b71Sopenharmony_ci| TIMER_TYPE_WAKEUP   | number | 2    | Wakeup type.                        |
27e41f4b71Sopenharmony_ci| TIMER_TYPE_EXACT    | number | 4    | Exact type.                        |
28e41f4b71Sopenharmony_ci| TIMER_TYPE_IDLE     | number | 8    | Idle timer type (supported only for system services, but not applications) |
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci ## TimerOptions
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ciDefines the initialization options for **createTimer**.
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci**System capability**: SystemCapability.MiscServices.Time
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci| Name     | Type                                         | Mandatory| Description                                                                                                                             |
37e41f4b71Sopenharmony_ci| --------- | --------------------------------------------- | ---- |---------------------------------------------------------------------------------------------------------------------------------|
38e41f4b71Sopenharmony_ci| 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 timer type (supported only for system services, but not applications).|
39e41f4b71Sopenharmony_ci| repeat    | boolean                                       | Yes  | Whether the timer is a repeating timer.<br>The value **true** means that the timer is a repeating timer, and **false** means that the timer is a one-shot timer.                                                                                           |
40e41f4b71Sopenharmony_ci| interval  | number                                        | No  | Repeat interval.<br>For a repeating timer, the value must be greater than 5000 ms. For a one-shot timer, the value is **0**.                                                                      |
41e41f4b71Sopenharmony_ci| wantAgent | WantAgent | No  | **WantAgent** object of the notification to be sent when the timer expires. (An application MainAbility can be started, but not a Service ability.)                                                               |
42e41f4b71Sopenharmony_ci| callback  | void                                          | No | Callback to be executed by the user.                                                                                                                    |
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci## systemTimer.createTimer
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_cicreateTimer(options: TimerOptions, callback: AsyncCallback&lt;number&gt;): void
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ciCreates a timer. This API uses an asynchronous callback to return the result.
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci**System capability**: SystemCapability.MiscServices.Time
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci**Parameters**
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci| Name  | Type                         | Mandatory| Description                                                        |
56e41f4b71Sopenharmony_ci| -------- | ----------------------------- | ---- | ------------------------------------------------------------ |
57e41f4b71Sopenharmony_ci| options  | [TimerOptions](#timeroptions) | Yes  | Timer initialization options, including the timer type, whether the timer is a repeating timer, interval, and **WantAgent** options.|
58e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;number>      | Yes  | Callback used to return the timer ID.                                  |
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci**Error codes**
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ciFor details about the error codes, see [Time and Time Zone Service Error Codes](./errorcode-time.md).
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci| ID| Error Message                                                                                                                                        |
65e41f4b71Sopenharmony_ci|-------|----------------------------------------------------------------------------------------------------------------------------------------------|
66e41f4b71Sopenharmony_ci| 202   | Permission verification failed. A non-system application calls a system API.                                                                 |
67e41f4b71Sopenharmony_ci| 401   | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. |
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci**Example**
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_ci```ts
72e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_cilet options: systemTimer.TimerOptions = {
75e41f4b71Sopenharmony_ci  type: systemTimer.TIMER_TYPE_REALTIME,
76e41f4b71Sopenharmony_ci  repeat: false
77e41f4b71Sopenharmony_ci};
78e41f4b71Sopenharmony_citry {
79e41f4b71Sopenharmony_ci  systemTimer.createTimer(options, (error: BusinessError, timerId: Number) => {
80e41f4b71Sopenharmony_ci    if (error) {
81e41f4b71Sopenharmony_ci      console.info(`Failed to create a timer. Message: ${error.message}, code: ${error.code}`);
82e41f4b71Sopenharmony_ci      return;
83e41f4b71Sopenharmony_ci    }
84e41f4b71Sopenharmony_ci    console.info(`Succeeded in creating a timer. timerId: ${timerId}`);
85e41f4b71Sopenharmony_ci  });
86e41f4b71Sopenharmony_ci} catch(e) {
87e41f4b71Sopenharmony_ci  let error = e as BusinessError;
88e41f4b71Sopenharmony_ci  console.info(`Failed to create a timer. Message: ${error.message}, code: ${error.code}`);
89e41f4b71Sopenharmony_ci}
90e41f4b71Sopenharmony_ci```
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci## systemTimer.createTimer
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_cicreateTimer(options: TimerOptions): Promise&lt;number&gt;
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ciCreates a timer. This API uses a promise to return the result.
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ci**System capability**: SystemCapability.MiscServices.Time
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci**Parameters**
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci| Name | Type                         | Mandatory| Description                                                        |
104e41f4b71Sopenharmony_ci| ------- | ----------------------------- | ---- | ------------------------------------------------------------ |
105e41f4b71Sopenharmony_ci| options | [TimerOptions](#timeroptions) | Yes  | Timer initialization options, including the timer type, whether the timer is a repeating timer, interval, and **WantAgent** options.|
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ci**Return value**
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci| Type                 | Description                         |
110e41f4b71Sopenharmony_ci| --------------------- | ----------------------------- |
111e41f4b71Sopenharmony_ci| Promise&lt;number&gt; | Promise used to return the timer ID.|
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci**Error codes**
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ciFor details about the error codes, see [Time and Time Zone Service Error Codes](./errorcode-time.md).
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci| ID| Error Message                                                                                                       |
118e41f4b71Sopenharmony_ci|-------|-------------------------------------------------------------------------------------------------------------|
119e41f4b71Sopenharmony_ci| 202   | Permission verification failed. A non-system application calls a system API.                                |
120e41f4b71Sopenharmony_ci| 401   | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ci**Example**
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci```ts
125e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_cilet options: systemTimer.TimerOptions = {
128e41f4b71Sopenharmony_ci  type: systemTimer.TIMER_TYPE_REALTIME,
129e41f4b71Sopenharmony_ci  repeat:false
130e41f4b71Sopenharmony_ci};
131e41f4b71Sopenharmony_citry {
132e41f4b71Sopenharmony_ci  systemTimer.createTimer(options).then((timerId: Number) => {
133e41f4b71Sopenharmony_ci    console.info(`Succeeded in creating a timer. timerId: ${timerId}`);
134e41f4b71Sopenharmony_ci  }).catch((error: BusinessError) => {
135e41f4b71Sopenharmony_ci    console.info(`Failed to create a timer. Message: ${error.message}, code: ${error.code}`);
136e41f4b71Sopenharmony_ci  });
137e41f4b71Sopenharmony_ci} catch(e) {
138e41f4b71Sopenharmony_ci  let error = e as BusinessError;
139e41f4b71Sopenharmony_ci  console.info(`Failed to create a timer. Message: ${error.message}, code: ${error.code}`);
140e41f4b71Sopenharmony_ci}
141e41f4b71Sopenharmony_ci```
142e41f4b71Sopenharmony_ci
143e41f4b71Sopenharmony_ci## systemTimer.startTimer
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_cistartTimer(timer: number, triggerTime: number, callback: AsyncCallback&lt;void&gt;): void
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ciStarts a timer. This API uses an asynchronous callback to return the result.
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ci**System capability**: SystemCapability.MiscServices.Time
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_ci**Parameters**
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ci| Name     | Type                  | Mandatory| Description                          |
154e41f4b71Sopenharmony_ci| ----------- | ---------------------- | ---- | ------------------------------ |
155e41f4b71Sopenharmony_ci| timer       | number                 | Yes  | ID of the timer.                  |
156e41f4b71Sopenharmony_ci| triggerTime | number                 | Yes  | Time when the timer is triggered, in milliseconds.|
157e41f4b71Sopenharmony_ci| callback    | AsyncCallback&lt;void> | Yes  | Callback used to return the result.                    |
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ci**Error codes**
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ciFor details about the error codes, see [Time and Time Zone Service Error Codes](./errorcode-time.md).
162e41f4b71Sopenharmony_ci
163e41f4b71Sopenharmony_ci| ID| Error Message                                                                                                       |
164e41f4b71Sopenharmony_ci|-------|-------------------------------------------------------------------------------------------------------------|
165e41f4b71Sopenharmony_ci| 202   | Permission verification failed. A non-system application calls a system API.                                |
166e41f4b71Sopenharmony_ci| 401   | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_ci**Example**
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ci```ts
171e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
172e41f4b71Sopenharmony_ci
173e41f4b71Sopenharmony_cilet options: systemTimer.TimerOptions = {
174e41f4b71Sopenharmony_ci  type: systemTimer.TIMER_TYPE_REALTIME,
175e41f4b71Sopenharmony_ci  repeat:false
176e41f4b71Sopenharmony_ci}
177e41f4b71Sopenharmony_cilet triggerTime = new Date().getTime();
178e41f4b71Sopenharmony_citriggerTime += 3000;
179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_citry {
181e41f4b71Sopenharmony_ci  systemTimer.createTimer(options).then((timerId: number) => {
182e41f4b71Sopenharmony_ci    systemTimer.startTimer(timerId, triggerTime, (error: BusinessError) => {
183e41f4b71Sopenharmony_ci      if (error) {
184e41f4b71Sopenharmony_ci        console.info(`Failed to start the timer. Message: ${error.message}, code: ${error.code}`);
185e41f4b71Sopenharmony_ci        return;
186e41f4b71Sopenharmony_ci      }
187e41f4b71Sopenharmony_ci      console.info(`Succeeded in starting the timer.`);
188e41f4b71Sopenharmony_ci    });
189e41f4b71Sopenharmony_ci    console.info(`Succeeded in creating a timer. timerId: ${timerId}`);
190e41f4b71Sopenharmony_ci  }).catch((error: BusinessError) => {
191e41f4b71Sopenharmony_ci    console.info(`Failed to create a timer. Message: ${error.message}, code: ${error.code}`);
192e41f4b71Sopenharmony_ci  });
193e41f4b71Sopenharmony_ci} catch(e) {
194e41f4b71Sopenharmony_ci  let error = e as BusinessError;
195e41f4b71Sopenharmony_ci  console.info(`Failed to create a timer. Message: ${error.message}, code: ${error.code}`);
196e41f4b71Sopenharmony_ci}
197e41f4b71Sopenharmony_ci```
198e41f4b71Sopenharmony_ci
199e41f4b71Sopenharmony_ci## systemTimer.startTimer
200e41f4b71Sopenharmony_ci
201e41f4b71Sopenharmony_cistartTimer(timer: number, triggerTime: number): Promise&lt;void&gt;
202e41f4b71Sopenharmony_ci
203e41f4b71Sopenharmony_ciStarts a timer. This API uses a promise to return the result.
204e41f4b71Sopenharmony_ci
205e41f4b71Sopenharmony_ci**System capability**: SystemCapability.MiscServices.Time
206e41f4b71Sopenharmony_ci
207e41f4b71Sopenharmony_ci**Parameters**
208e41f4b71Sopenharmony_ci
209e41f4b71Sopenharmony_ci| Name     | Type  | Mandatory| Description                          |
210e41f4b71Sopenharmony_ci| ----------- | ------ | ---- | ------------------------------ |
211e41f4b71Sopenharmony_ci| timer       | number | Yes  | ID of the timer.                  |
212e41f4b71Sopenharmony_ci| triggerTime | number | Yes  | Time when the timer is triggered, in milliseconds.|
213e41f4b71Sopenharmony_ci
214e41f4b71Sopenharmony_ci**Return value**
215e41f4b71Sopenharmony_ci
216e41f4b71Sopenharmony_ci| Type          | Description                     |
217e41f4b71Sopenharmony_ci| -------------- | ------------------------- |
218e41f4b71Sopenharmony_ci| Promise\<void> | Promise that returns no value.|
219e41f4b71Sopenharmony_ci
220e41f4b71Sopenharmony_ci**Error codes**
221e41f4b71Sopenharmony_ci
222e41f4b71Sopenharmony_ciFor details about the error codes, see [Time and Time Zone Service Error Codes](./errorcode-time.md).
223e41f4b71Sopenharmony_ci
224e41f4b71Sopenharmony_ci| ID| Error Message                                                                                                       |
225e41f4b71Sopenharmony_ci|-------|-------------------------------------------------------------------------------------------------------------|
226e41f4b71Sopenharmony_ci| 202   | Permission verification failed. A non-system application calls a system API.                                |
227e41f4b71Sopenharmony_ci| 401   | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
228e41f4b71Sopenharmony_ci
229e41f4b71Sopenharmony_ci**Example**
230e41f4b71Sopenharmony_ci
231e41f4b71Sopenharmony_ci```ts
232e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
233e41f4b71Sopenharmony_ci
234e41f4b71Sopenharmony_cilet options: systemTimer.TimerOptions = {
235e41f4b71Sopenharmony_ci  type: systemTimer.TIMER_TYPE_REALTIME,
236e41f4b71Sopenharmony_ci  repeat:false
237e41f4b71Sopenharmony_ci}
238e41f4b71Sopenharmony_cilet triggerTime = new Date().getTime();
239e41f4b71Sopenharmony_citriggerTime += 3000;
240e41f4b71Sopenharmony_ci
241e41f4b71Sopenharmony_citry {
242e41f4b71Sopenharmony_ci  systemTimer.createTimer(options).then((timerId: number) => {
243e41f4b71Sopenharmony_ci    systemTimer.startTimer(timerId, triggerTime).then(() => {
244e41f4b71Sopenharmony_ci      console.info(`Succeeded in starting the timer.`);
245e41f4b71Sopenharmony_ci    }).catch((error: BusinessError) => {
246e41f4b71Sopenharmony_ci      console.info(`Failed to start the timer. Message: ${error.message}, code: ${error.code}`);
247e41f4b71Sopenharmony_ci    });
248e41f4b71Sopenharmony_ci    console.info(`Succeeded in creating a timer. timerId: ${timerId}`);
249e41f4b71Sopenharmony_ci  }).catch((error: BusinessError) => {
250e41f4b71Sopenharmony_ci    console.info(`Failed to create a timer. Message: ${error.message}, code: ${error.code}`);
251e41f4b71Sopenharmony_ci  });
252e41f4b71Sopenharmony_ci} catch(e) {
253e41f4b71Sopenharmony_ci  let error = e as BusinessError;
254e41f4b71Sopenharmony_ci  console.info(`Failed to create a timer. Message: ${error.message}, code: ${error.code}`);
255e41f4b71Sopenharmony_ci}
256e41f4b71Sopenharmony_ci```
257e41f4b71Sopenharmony_ci
258e41f4b71Sopenharmony_ci## systemTimer.stopTimer
259e41f4b71Sopenharmony_ci
260e41f4b71Sopenharmony_cistopTimer(timer: number, callback: AsyncCallback&lt;void&gt;): void
261e41f4b71Sopenharmony_ci
262e41f4b71Sopenharmony_ciStops a timer. This API uses an asynchronous callback to return the result.
263e41f4b71Sopenharmony_ci
264e41f4b71Sopenharmony_ci**System capability**: SystemCapability.MiscServices.Time
265e41f4b71Sopenharmony_ci
266e41f4b71Sopenharmony_ci**Parameters**
267e41f4b71Sopenharmony_ci
268e41f4b71Sopenharmony_ci| Name  | Type                  | Mandatory| Description        |
269e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | ------------ |
270e41f4b71Sopenharmony_ci| timer    | number                 | Yes  | ID of the timer.|
271e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;void> | Yes  | Callback used to return the result.  |
272e41f4b71Sopenharmony_ci
273e41f4b71Sopenharmony_ci**Error codes**
274e41f4b71Sopenharmony_ci
275e41f4b71Sopenharmony_ciFor details about the error codes, see [Time and Time Zone Service Error Codes](./errorcode-time.md).
276e41f4b71Sopenharmony_ci
277e41f4b71Sopenharmony_ci| ID| Error Message                                                                                                       |
278e41f4b71Sopenharmony_ci|-------|-------------------------------------------------------------------------------------------------------------|
279e41f4b71Sopenharmony_ci| 202   | Permission verification failed. A non-system application calls a system API.                                |
280e41f4b71Sopenharmony_ci| 401   | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
281e41f4b71Sopenharmony_ci
282e41f4b71Sopenharmony_ci**Example**
283e41f4b71Sopenharmony_ci
284e41f4b71Sopenharmony_ci```ts
285e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
286e41f4b71Sopenharmony_ci
287e41f4b71Sopenharmony_cilet options: systemTimer.TimerOptions = {
288e41f4b71Sopenharmony_ci  type: systemTimer.TIMER_TYPE_REALTIME,
289e41f4b71Sopenharmony_ci  repeat:false
290e41f4b71Sopenharmony_ci}
291e41f4b71Sopenharmony_cilet triggerTime = new Date().getTime();
292e41f4b71Sopenharmony_citriggerTime += 3000;
293e41f4b71Sopenharmony_ci
294e41f4b71Sopenharmony_citry {
295e41f4b71Sopenharmony_ci  systemTimer.createTimer(options).then((timerId: number) => {
296e41f4b71Sopenharmony_ci    systemTimer.startTimer(timerId, triggerTime);
297e41f4b71Sopenharmony_ci    systemTimer.stopTimer(timerId, (error: BusinessError) => {
298e41f4b71Sopenharmony_ci      if (error) {
299e41f4b71Sopenharmony_ci        console.info(`Failed to stop the timer. Message: ${error.message}, code: ${error.code}`);
300e41f4b71Sopenharmony_ci        return;
301e41f4b71Sopenharmony_ci      }
302e41f4b71Sopenharmony_ci    console.info(`Succeeded in stopping the timer.`);
303e41f4b71Sopenharmony_ci    });
304e41f4b71Sopenharmony_ci    console.info(`Succeeded in creating a timer. timerId: ${timerId}`);
305e41f4b71Sopenharmony_ci  }).catch((error: BusinessError) => {
306e41f4b71Sopenharmony_ci    console.info(`Failed to create a timer. Message: ${error.message}, code: ${error.code}`);
307e41f4b71Sopenharmony_ci  });
308e41f4b71Sopenharmony_ci} catch(e) {
309e41f4b71Sopenharmony_ci  let error = e as BusinessError;
310e41f4b71Sopenharmony_ci  console.info(`Failed to create a timer. Message: ${error.message}, code: ${error.code}`);
311e41f4b71Sopenharmony_ci}
312e41f4b71Sopenharmony_ci```
313e41f4b71Sopenharmony_ci
314e41f4b71Sopenharmony_ci## systemTimer.stopTimer
315e41f4b71Sopenharmony_ci
316e41f4b71Sopenharmony_cistopTimer(timer: number): Promise&lt;void&gt;
317e41f4b71Sopenharmony_ci
318e41f4b71Sopenharmony_ciStops a timer. This API uses a promise to return the result.
319e41f4b71Sopenharmony_ci
320e41f4b71Sopenharmony_ci**System capability**: SystemCapability.MiscServices.Time
321e41f4b71Sopenharmony_ci
322e41f4b71Sopenharmony_ci**Parameters**
323e41f4b71Sopenharmony_ci
324e41f4b71Sopenharmony_ci| Name| Type  | Mandatory| Description        |
325e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------ |
326e41f4b71Sopenharmony_ci| timer  | number | Yes  | ID of the timer.|
327e41f4b71Sopenharmony_ci
328e41f4b71Sopenharmony_ci**Return value**
329e41f4b71Sopenharmony_ci
330e41f4b71Sopenharmony_ci| Type          | Description                     |
331e41f4b71Sopenharmony_ci| -------------- | ------------------------- |
332e41f4b71Sopenharmony_ci| Promise\<void> | Promise that returns no value.|
333e41f4b71Sopenharmony_ci
334e41f4b71Sopenharmony_ci**Error codes**
335e41f4b71Sopenharmony_ci
336e41f4b71Sopenharmony_ciFor details about the error codes, see [Time and Time Zone Service Error Codes](./errorcode-time.md).
337e41f4b71Sopenharmony_ci
338e41f4b71Sopenharmony_ci| ID| Error Message                                                                                                       |
339e41f4b71Sopenharmony_ci|-------|-------------------------------------------------------------------------------------------------------------|
340e41f4b71Sopenharmony_ci| 202   | Permission verification failed. A non-system application calls a system API.                                |
341e41f4b71Sopenharmony_ci| 401   | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
342e41f4b71Sopenharmony_ci
343e41f4b71Sopenharmony_ci**Example**
344e41f4b71Sopenharmony_ci
345e41f4b71Sopenharmony_ci```ts
346e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
347e41f4b71Sopenharmony_ci
348e41f4b71Sopenharmony_cilet options: systemTimer.TimerOptions = {
349e41f4b71Sopenharmony_ci  type: systemTimer.TIMER_TYPE_REALTIME,
350e41f4b71Sopenharmony_ci  repeat:false
351e41f4b71Sopenharmony_ci}
352e41f4b71Sopenharmony_cilet triggerTime = new Date().getTime();
353e41f4b71Sopenharmony_citriggerTime += 3000;
354e41f4b71Sopenharmony_ci
355e41f4b71Sopenharmony_citry {
356e41f4b71Sopenharmony_ci  systemTimer.createTimer(options).then((timerId: number) => {
357e41f4b71Sopenharmony_ci    systemTimer.startTimer(timerId, triggerTime);
358e41f4b71Sopenharmony_ci    systemTimer.stopTimer(timerId).then(() => {
359e41f4b71Sopenharmony_ci      console.info(`Succeeded in stopping the timer.`);
360e41f4b71Sopenharmony_ci    }).catch((error: BusinessError) => {
361e41f4b71Sopenharmony_ci      console.info(`Failed to stop the timer. Message: ${error.message}, code: ${error.code}`);
362e41f4b71Sopenharmony_ci    });
363e41f4b71Sopenharmony_ci    console.info(`Succeeded in creating a timer. timerId: ${timerId}`);
364e41f4b71Sopenharmony_ci  }).catch((error: BusinessError) => {
365e41f4b71Sopenharmony_ci    console.info(`Failed to create a timer. Message: ${error.message}, code: ${error.code}`);
366e41f4b71Sopenharmony_ci  });
367e41f4b71Sopenharmony_ci} catch(e) {
368e41f4b71Sopenharmony_ci  let error = e as BusinessError;
369e41f4b71Sopenharmony_ci  console.info(`Failed to create a timer. Message: ${error.message}, code: ${error.code}`);
370e41f4b71Sopenharmony_ci}
371e41f4b71Sopenharmony_ci```
372e41f4b71Sopenharmony_ci
373e41f4b71Sopenharmony_ci## systemTimer.destroyTimer
374e41f4b71Sopenharmony_ci
375e41f4b71Sopenharmony_cidestroyTimer(timer: number, callback: AsyncCallback&lt;void&gt;): void
376e41f4b71Sopenharmony_ci
377e41f4b71Sopenharmony_ciDestroys a timer. This API uses an asynchronous callback to return the result.
378e41f4b71Sopenharmony_ci
379e41f4b71Sopenharmony_ci**System capability**: SystemCapability.MiscServices.Time
380e41f4b71Sopenharmony_ci
381e41f4b71Sopenharmony_ci**Parameters**
382e41f4b71Sopenharmony_ci
383e41f4b71Sopenharmony_ci| Name  | Type                  | Mandatory| Description        |
384e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | ------------ |
385e41f4b71Sopenharmony_ci| timer    | number                 | Yes  | ID of the timer.|
386e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;void> | Yes  | Callback used to return the result.  |
387e41f4b71Sopenharmony_ci
388e41f4b71Sopenharmony_ci**Error codes**
389e41f4b71Sopenharmony_ci
390e41f4b71Sopenharmony_ciFor details about the error codes, see [Time and Time Zone Service Error Codes](./errorcode-time.md).
391e41f4b71Sopenharmony_ci
392e41f4b71Sopenharmony_ci| ID| Error Message                                                                                                       |
393e41f4b71Sopenharmony_ci|-------|-------------------------------------------------------------------------------------------------------------|
394e41f4b71Sopenharmony_ci| 202   | Permission verification failed. A non-system application calls a system API.                                |
395e41f4b71Sopenharmony_ci| 401   | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
396e41f4b71Sopenharmony_ci
397e41f4b71Sopenharmony_ci**Example**
398e41f4b71Sopenharmony_ci
399e41f4b71Sopenharmony_ci```ts
400e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
401e41f4b71Sopenharmony_ci
402e41f4b71Sopenharmony_cilet options: systemTimer.TimerOptions = {
403e41f4b71Sopenharmony_ci  type: systemTimer.TIMER_TYPE_REALTIME,
404e41f4b71Sopenharmony_ci  repeat:false
405e41f4b71Sopenharmony_ci}
406e41f4b71Sopenharmony_cilet triggerTime = new Date().getTime();
407e41f4b71Sopenharmony_citriggerTime += 3000;
408e41f4b71Sopenharmony_ci
409e41f4b71Sopenharmony_citry {
410e41f4b71Sopenharmony_ci  systemTimer.createTimer(options).then((timerId: number) => {
411e41f4b71Sopenharmony_ci    systemTimer.startTimer(timerId, triggerTime);
412e41f4b71Sopenharmony_ci    systemTimer.stopTimer(timerId);
413e41f4b71Sopenharmony_ci    systemTimer.destroyTimer(timerId, (error: BusinessError) => {
414e41f4b71Sopenharmony_ci      if (error) {
415e41f4b71Sopenharmony_ci        console.info(`Failed to destroy the timer. Message: ${error.message}, code: ${error.code}`);
416e41f4b71Sopenharmony_ci        return;
417e41f4b71Sopenharmony_ci      }
418e41f4b71Sopenharmony_ci    console.info(`Succeeded in destroying the timer.`);
419e41f4b71Sopenharmony_ci    });
420e41f4b71Sopenharmony_ci    console.info(`Succeeded in creating a timer. timerId: ${timerId}`);
421e41f4b71Sopenharmony_ci  }).catch((error: BusinessError) => {
422e41f4b71Sopenharmony_ci    console.info(`Failed to create a timer. Message: ${error.message}, code: ${error.code}`);
423e41f4b71Sopenharmony_ci  });
424e41f4b71Sopenharmony_ci} catch(e) {
425e41f4b71Sopenharmony_ci  let error = e as BusinessError;
426e41f4b71Sopenharmony_ci  console.info(`Failed to create a timer. Message: ${error.message}, code: ${error.code}`);
427e41f4b71Sopenharmony_ci}
428e41f4b71Sopenharmony_ci```
429e41f4b71Sopenharmony_ci
430e41f4b71Sopenharmony_ci## systemTimer.destroyTimer
431e41f4b71Sopenharmony_ci
432e41f4b71Sopenharmony_cidestroyTimer(timer: number): Promise&lt;void&gt;
433e41f4b71Sopenharmony_ci
434e41f4b71Sopenharmony_ciDestroys a timer. This API uses a promise to return the result.
435e41f4b71Sopenharmony_ci
436e41f4b71Sopenharmony_ci**System capability**: SystemCapability.MiscServices.Time
437e41f4b71Sopenharmony_ci
438e41f4b71Sopenharmony_ci**Parameters**
439e41f4b71Sopenharmony_ci
440e41f4b71Sopenharmony_ci| Name| Type  | Mandatory| Description        |
441e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------ |
442e41f4b71Sopenharmony_ci| timer  | number | Yes  | ID of the timer.|
443e41f4b71Sopenharmony_ci
444e41f4b71Sopenharmony_ci**Return value**
445e41f4b71Sopenharmony_ci
446e41f4b71Sopenharmony_ci| Type          | Description                     |
447e41f4b71Sopenharmony_ci| -------------- | ------------------------- |
448e41f4b71Sopenharmony_ci| Promise\<void> | Promise that returns no value.|
449e41f4b71Sopenharmony_ci
450e41f4b71Sopenharmony_ci**Error codes**
451e41f4b71Sopenharmony_ci
452e41f4b71Sopenharmony_ciFor details about the error codes, see [Time and Time Zone Service Error Codes](./errorcode-time.md).
453e41f4b71Sopenharmony_ci
454e41f4b71Sopenharmony_ci| ID| Error Message                                                                                                       |
455e41f4b71Sopenharmony_ci|-------|-------------------------------------------------------------------------------------------------------------|
456e41f4b71Sopenharmony_ci| 202   | Permission verification failed. A non-system application calls a system API.                                |
457e41f4b71Sopenharmony_ci| 401   | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
458e41f4b71Sopenharmony_ci
459e41f4b71Sopenharmony_ci**Example**
460e41f4b71Sopenharmony_ci
461e41f4b71Sopenharmony_ci```ts
462e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
463e41f4b71Sopenharmony_ci
464e41f4b71Sopenharmony_cilet options: systemTimer.TimerOptions = {
465e41f4b71Sopenharmony_ci  type: systemTimer.TIMER_TYPE_REALTIME,
466e41f4b71Sopenharmony_ci  repeat:false
467e41f4b71Sopenharmony_ci}
468e41f4b71Sopenharmony_cilet triggerTime = new Date().getTime();
469e41f4b71Sopenharmony_citriggerTime += 3000;
470e41f4b71Sopenharmony_ci
471e41f4b71Sopenharmony_citry {
472e41f4b71Sopenharmony_ci  systemTimer.createTimer(options).then((timerId: number) => {
473e41f4b71Sopenharmony_ci    systemTimer.startTimer(timerId, triggerTime);
474e41f4b71Sopenharmony_ci    systemTimer.stopTimer(timerId);
475e41f4b71Sopenharmony_ci    systemTimer.destroyTimer(timerId).then(() => {
476e41f4b71Sopenharmony_ci      console.info(`Succeeded in destroying the timer.`);
477e41f4b71Sopenharmony_ci    }).catch((error: BusinessError) => {
478e41f4b71Sopenharmony_ci      console.info(`Failed to destroy the timer. Message: ${error.message}, code: ${error.code}`);
479e41f4b71Sopenharmony_ci    });
480e41f4b71Sopenharmony_ci    console.info(`Succeeded in creating a timer. timerId: ${timerId}`);
481e41f4b71Sopenharmony_ci  }).catch((error: BusinessError) => {
482e41f4b71Sopenharmony_ci    console.info(`Failed to create a timer. Message: ${error.message}, code: ${error.code}`);
483e41f4b71Sopenharmony_ci  });
484e41f4b71Sopenharmony_ci} catch(e) {
485e41f4b71Sopenharmony_ci  let error = e as BusinessError;
486e41f4b71Sopenharmony_ci  console.info(`Failed to create a timer. Message: ${error.message}, code: ${error.code}`);
487e41f4b71Sopenharmony_ci}
488e41f4b71Sopenharmony_ci```
489