README.md
1# Timing and Time<a name="EN-US_TOPIC_0000001115554184"></a>
2
3- [Introduction](#section11660541593)
4- [Directory Structure](#section161941989596)
5- [Usage](#section38521239153117)
6 - [Available JS APIs](#section11908203714422)
7 - [Sample Code](#section9938411124317)
8
9- [Repositories Involved](#section1371113476307)
10
11## Introduction<a name="section11660541593"></a>
12
13The timing and time module provides APIs for managing the system time.
14
15**Figure 1** Subsystem architecture<a name="fig143011012341"></a>
16![](figures/subsystem_architecture.png "subsystem-architecture")
17
18## Directory Structure<a name="section161941989596"></a>
19
20```
21/base/time/time_service
22├── etc # Process configuration files
23├── figures # Architecture diagram
24├── interfaces # APIs for external systems and applications
25│ └── innerkits # APIs between services
26│ └── kits # APIs
27├── profile # System service configuration files
28└── services # Service implementation
29```
30
31
32## Usage<a name="section38521239153117"></a>
33
34### Available JS APIs<a name="section11908203714422"></a>
35
36**Table 1** Major functions of systemTime
37
38<a name="table033515471012"></a>
39<table><thead align="left"><tr id="row143351854201012"><th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.1"><p id="p103351154121010"><a name="p103351154121010"></a><a name="p103351154121010"></a>Interface name</p>
40</th>
41<th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.2"><p id="p1033585416105"><a name="p1033585416105"></a><a name="p1033585416105"></a>describe</p>
42</th>
43</tr>
44</thead>
45<tbody><tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function setTime(time : number) : Promise<boolean></p>
46</td>
47<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>Set the system time (1970-01-01 to the present in milliseconds), Promise method</p>
48</td>
49</tr>
50<tr id="row13335054111018"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p12832214151418"><a name="p12832214151418"></a><a name="p12832214151418"></a>function setTime(time : number, callback : AsyncCallback<boolean>) : void</p>
51</td>
52<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p3335145451011"><a name="p3335145451011"></a><a name="p3335145451011"></a>Set the system time (1970-01-01 to the present in milliseconds), callback mode</p>
53</td>
54</tr>
55<tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function setDate(date: Date, callback: AsyncCallback<boolean>): void;</p>
56</td>
57<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>Set the system time (Date format), Promise method</p>
58</td>
59</tr>
60<tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function setDate(date: Date): Promise<boolean></p>
61</td>
62<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>Set system time (Date format), callback method</p>
63</td>
64</tr>
65<tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function setTimezone(timezone: string, callback: AsyncCallback<boolean>): void</p>
66</td>
67<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>Set the system time zone, callback method</p>
68</td>
69</tr>
70<tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function setTimezone(timezone: string): Promise<boolean></p>
71</td>
72<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>Set the system time zone, Promise method</p>
73</td>
74</tr>
75</tbody>
76</table>
77
78
79**表 2** Major functions of systemTimer
80
81<a name="table033515471012"></a>
82<table><thead align="left"><tr id="row143351854201012"><th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.1"><p id="p103351154121010"><a name="p103351154121010"></a><a name="p103351154121010"></a>Interface name</p>
83</th>
84<th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.2"><p id="p1033585416105"><a name="p1033585416105"></a><a name="p1033585416105"></a>describe</p>
85</th>
86</tr>
87</thead>
88<tbody><tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function createTimer(options: TimerOptions, callback: AsyncCallback<number>): void</p>
89</td>
90<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>Create timer, callback method</p>
91</td>
92</tr>
93<tr id="row13335054111018"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p12832214151418"><a name="p12832214151418"></a><a name="p12832214151418"></a>function createTimer(options: TimerOptions): Promise<number></p>
94</td>
95<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p3335145451011"><a name="p3335145451011"></a><a name="p3335145451011"></a>Create timer, promise method</p>
96</td>
97</tr>
98<tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function startTimer(timer: number, triggerTime: number, callback: AsyncCallback<boolean>): void</p>
99</td>
100<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>Start the timer, callback mode</p>
101</td>
102</tr>
103<tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function startTimer(timer: number, triggerTime: number): Promise<boolean></p>
104</td>
105<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>Start the timer, promise mode</p>
106</td>
107</tr>
108<tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function stopTimer(timer: number, callback: AsyncCallback<boolean>): void</p>
109</td>
110<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>Stop the timer, callback mode</p>
111</td>
112</tr>
113<tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function stopTimer(timer: number): Promise<boolean></p>
114</td>
115<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>Stop the timer, promise mode</p>
116</td>
117</tr>
118<tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function destroyTimer(timer: number, callback: AsyncCallback<boolean>): void</p>
119</td>
120<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>Destroy the timer, callback method</p>
121</td>
122</tr>
123<tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function destroyTimer(timer: number): Promise<boolean></p>
124</td>
125<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>Destroy the timer, the promise method</p>
126</td>
127</tr>
128</tbody>
129</table>
130
131**表 3** parameter TimerOptions description of systemTimer
132
133<a name="table033515471012"></a>
134<table><thead align="left"><tr id="row143351854201012"><th class="cellrowborder" valign="top" width="20%" id="mcps1.2.3.1.1"><p id="p103351154121010"><a name="p103351154121010"></a><a name="p103351154121010"></a>name</p>
135</th>
136<th class="cellrowborder" valign="top" width="10%" id="mcps1.2.3.1.2"><p id="p1033585416105"><a name="p1033585416105"></a><a name="p1033585416105"></a>type</p>
137</th>
138<th class="cellrowborder" valign="top" width="70%" id="mcps1.2.3.1.3"><p id="p1033585416105"><a name="p1033585416105"></a><a name="p1033585416105"></a>illustrate</p>
139</th>
140</tr>
141</thead>
142<tbody><tr id="row204321219393"><td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>type</p>
143</td>
144<td class="cellrowborder" valign="top" width="10%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>number</p>
145</td>
146<td class="cellrowborder" valign="top" width="70%" headers="mcps1.2.3.1.3 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>TIMER_TYPE_REALTIME: Set as the system startup time timer, otherwise it is the walltime timer;
147 TIMER_TYPE_WAKEUP: Set to wake-up timer, otherwise it is non-wake-up;
148 const TIMER_TYPE_EXACT: Set as a precision timer, otherwise it is a non-precision timer;
149 const TIMER_TYPE_IDLE: Set to IDLE mode timer, otherwise it is non-IDLE mode timer (not supported yet)
150</p>
151</td>
152</tr>
153<tr id="row204321219393"><td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>repeat</p>
154</td>
155<td class="cellrowborder" valign="top" width="10%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>boolean</p>
156</td>
157<td class="cellrowborder" valign="top" width="70%" headers="mcps1.2.3.1.3 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>true Is a cyclic timer, false is a single timer</p>
158</td>
159</tr>
160<tr id="row204321219393"><td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>interval</p>
161</td>
162<td class="cellrowborder" valign="top" width="10%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>number</p>
163</td>
164<td class="cellrowborder" valign="top" width="70%" headers="mcps1.2.3.1.3 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>If it is a cyclic timer, the repeat value should be greater than 5000 milliseconds, and the non-repeated timer is set to 0</p>
165</td>
166</tr>
167<tr id="row204321219393"><td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>wantAgent</p>
168</td>
169<td class="cellrowborder" valign="top" width="10%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>wantAgent</p>
170</td>
171<td class="cellrowborder" valign="top" width="70%" headers="mcps1.2.3.1.3 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>Set the wantagent to notify, and notify when the timer expires</p>
172</td>
173</tr>
174<tr id="row204321219393"><td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>callback</p>
175</td>
176<td class="cellrowborder" valign="top" width="10%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a> => void</p>
177</td>
178<td class="cellrowborder" valign="top" width="70%" headers="mcps1.2.3.1.3 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>Set the callback function, which will be triggered after the timer expires</p>
179</td>
180</tr>
181</tbody>
182</table>
183
184### Sample Code<a name="section9938411124317"></a>
185
186Example fo using systemTime
187
188```
189// Import the module.
190import systemTime from '@ohos.systemTime';
191
192// Set the system time asynchronously with a Promise.
193var time = 1611081385000;
194systemTime.setTime(time)
195 .then((value) => {
196 console.log(`success to systemTime.setTime: ${value}`);
197 }).catch((err) => {
198 console.error(`failed to systemTime.setTime because ${err.message}`)
199 });
200
201// Set the system time asynchronously with a callback.
202var time = 1611081385000;
203systemTime.setTime(time, (err, value) => {
204 if (err) {
205 console.error(`failed to systemTime.setTime because ${err.message}`);
206 return;
207 }
208 console.log(`success to systemTime.setTime: ${value}`);
209 });
210```
211Example fo using systemTimer
212```
213// Import the module
214import systemTimer from '@ohos.systemTimer';
215
216console.log("start")
217var options:TimerOptions{
218 type:TIMER_TYPE_REALTIME,
219 repeat:false,
220 interval:Number.MAX_VALUE/2,
221 persistent:false
222}
223
224console.log("create timer")
225let timerId = systemTimer.Timer(options)
226console.log("start timer")
227let startTimerRes = systemTimer.startTimer(timerId, 100000)
228console.log("stop timer")
229let stopTimerRes = systemTimer.stopTimer(timerId)
230console.log("destroy timer")
231let destroyTimerRes = systemTimer.destroyTimer(timerId)
232console.log('end');
233
234
235```
236
237## Repositories Involved<a name="section1371113476307"></a>
238
239**Time/Timezone subsystem**
240
241[time\_time\_service](https://gitee.com/openharmony/time_time_service/tree/master/)
242
243
README_zh.md
1# 时间时区部件<a name="ZH-CN_TOPIC_0000001115554184"></a>
2
3- [简介](#section11660541593)
4- [目录](#section161941989596)
5- [说明](#section38521239153117)
6 - [js接口说明](#section11908203714422)
7 - [js接口使用说明](#section9938411124317)
8
9- [相关仓](#section1371113476307)
10
11## 简介<a name="section11660541593"></a>
12
13在整个OpenHarmony架构中提供管理系统时间时区和定时的能力,支持设置获取时间、日期、时区和系统定时器功能。
14
15**图 1** 子系统架构图<a name="fig143011012341"></a>
16![](figures/subsystem_architecture_zh.png "子系统架构图")
17
18## 目录<a name="section161941989596"></a>
19
20```
21/base/time/time_service
22├── etc # 组件包含的进程的配置文件
23├── figures # 构架图
24├── interfaces # 组件对外提供的接口代码
25│ └── innerkits # 服务间接口
26│ └── kits # 对应用提供的接口
27├── profile # 组件包含的系统服务的配置文件
28└── services # 时间服务实现
29```
30
31## 说明<a name="section38521239153117"></a>
32
33### js接口说明<a name="section11908203714422"></a>
34
35**表 1** js组件systemTime开放的主要方法
36
37<a name="table033515471012"></a>
38<table><thead align="left"><tr id="row143351854201012"><th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.1"><p id="p103351154121010"><a name="p103351154121010"></a><a name="p103351154121010"></a>接口名</p>
39</th>
40<th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.2"><p id="p1033585416105"><a name="p1033585416105"></a><a name="p1033585416105"></a>描述</p>
41</th>
42</tr>
43</thead>
44<tbody><tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function setTime(time : number) : Promise<boolean></p>
45</td>
46<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>设置系统时间(1970-01-01至今毫秒数),Promise方式</p>
47</td>
48</tr>
49<tr id="row13335054111018"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p12832214151418"><a name="p12832214151418"></a><a name="p12832214151418"></a>function setTime(time : number, callback : AsyncCallback<boolean>) : void</p>
50</td>
51<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p3335145451011"><a name="p3335145451011"></a><a name="p3335145451011"></a>设置系统时间(1970-01-01至今毫秒数),callback方式</p>
52</td>
53</tr>
54<tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function setDate(date: Date, callback: AsyncCallback<boolean>): void;</p>
55</td>
56<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>设置系统时间(Date格式),Promise方式</p>
57</td>
58</tr>
59<tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function setDate(date: Date): Promise<boolean></p>
60</td>
61<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>设置系统时间(Date格式),callback方式</p>
62</td>
63</tr>
64<tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function setTimezone(timezone: string, callback: AsyncCallback<boolean>): void</p>
65</td>
66<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>设置系统时区,callback方式</p>
67</td>
68</tr>
69<tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function setTimezone(timezone: string): Promise<boolean></p>
70</td>
71<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>设置系统时区,Promise方式</p>
72</td>
73</tr>
74</tbody>
75</table>
76
77
78**表 2** js组件systemTimer开放的主要方法
79
80<a name="table033515471012"></a>
81<table><thead align="left"><tr id="row143351854201012"><th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.1"><p id="p103351154121010"><a name="p103351154121010"></a><a name="p103351154121010"></a>接口名</p>
82</th>
83<th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.2"><p id="p1033585416105"><a name="p1033585416105"></a><a name="p1033585416105"></a>描述</p>
84</th>
85</tr>
86</thead>
87<tbody><tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function createTimer(options: TimerOptions, callback: AsyncCallback<number>): void</p>
88</td>
89<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>创建定时器,callback方式</p>
90</td>
91</tr>
92<tr id="row13335054111018"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p12832214151418"><a name="p12832214151418"></a><a name="p12832214151418"></a>function createTimer(options: TimerOptions): Promise<number></p>
93</td>
94<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p3335145451011"><a name="p3335145451011"></a><a name="p3335145451011"></a>创建定时器,promise方式</p>
95</td>
96</tr>
97<tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function startTimer(timer: number, triggerTime: number, callback: AsyncCallback<boolean>): void</p>
98</td>
99<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>开启定时器,callback方式</p>
100</td>
101</tr>
102<tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function startTimer(timer: number, triggerTime: number): Promise<boolean></p>
103</td>
104<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>开启定时器,promise方式</p>
105</td>
106</tr>
107<tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function stopTimer(timer: number, callback: AsyncCallback<boolean>): void</p>
108</td>
109<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>停止定时器,callback方式</p>
110</td>
111</tr>
112<tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function stopTimer(timer: number): Promise<boolean></p>
113</td>
114<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>停止定时器,promise方式</p>
115</td>
116</tr>
117<tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function destroyTimer(timer: number, callback: AsyncCallback<boolean>): void</p>
118</td>
119<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>销毁定时器,callback方式</p>
120</td>
121</tr>
122<tr id="row204321219393"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>function destroyTimer(timer: number): Promise<boolean></p>
123</td>
124<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>摧毁定时器,promise方式</p>
125</td>
126</tr>
127</tbody>
128</table>
129
130**表 3** systemTimer组件参数TimerOptions说明
131
132<a name="table033515471012"></a>
133<table><thead align="left"><tr id="row143351854201012"><th class="cellrowborder" valign="top" width="20%" id="mcps1.2.3.1.1"><p id="p103351154121010"><a name="p103351154121010"></a><a name="p103351154121010"></a>名称</p>
134</th>
135<th class="cellrowborder" valign="top" width="10%" id="mcps1.2.3.1.2"><p id="p1033585416105"><a name="p1033585416105"></a><a name="p1033585416105"></a>类型</p>
136</th>
137<th class="cellrowborder" valign="top" width="70%" id="mcps1.2.3.1.3"><p id="p1033585416105"><a name="p1033585416105"></a><a name="p1033585416105"></a>说明</p>
138</th>
139</tr>
140</thead>
141<tbody><tr id="row204321219393"><td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>type</p>
142</td>
143<td class="cellrowborder" valign="top" width="10%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>number</p>
144</td>
145<td class="cellrowborder" valign="top" width="70%" headers="mcps1.2.3.1.3 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>TIMER_TYPE_REALTIME: 设置为系统启动时间定时器,否则为walltime定时器;
146 TIMER_TYPE_WAKEUP: 设置为唤醒定时器,否则为非唤醒;
147 const TIMER_TYPE_EXACT: 设置为精准定时器,否则为非精准定时器;
148 const TIMER_TYPE_IDLE: 设置为IDLE模式定时器,否则为非IDLE模式定时器(暂不支持)
149</p>
150</td>
151</tr>
152<tr id="row204321219393"><td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>repeat</p>
153</td>
154<td class="cellrowborder" valign="top" width="10%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>boolean</p>
155</td>
156<td class="cellrowborder" valign="top" width="70%" headers="mcps1.2.3.1.3 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>true 为循环定时器,false为单次定时器</p>
157</td>
158</tr>
159<tr id="row204321219393"><td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>interval</p>
160</td>
161<td class="cellrowborder" valign="top" width="10%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>number</p>
162</td>
163<td class="cellrowborder" valign="top" width="70%" headers="mcps1.2.3.1.3 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>如果是循环定时器,repeat值应大于5000毫秒,非重复定时器置为0</p>
164</td>
165</tr>
166<tr id="row204321219393"><td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>wantAgent</p>
167</td>
168<td class="cellrowborder" valign="top" width="10%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>wantAgent</p>
169</td>
170<td class="cellrowborder" valign="top" width="70%" headers="mcps1.2.3.1.3 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>设置通知的wantagent,定时器到期后通知</p>
171</td>
172</tr>
173<tr id="row204321219393"><td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.3.1.1 "><p id="p1893413268144"><a name="p1893413268144"></a><a name="p1893413268144"></a>callback</p>
174</td>
175<td class="cellrowborder" valign="top" width="10%" headers="mcps1.2.3.1.2 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a> => void</p>
176</td>
177<td class="cellrowborder" valign="top" width="70%" headers="mcps1.2.3.1.3 "><p id="p18761104812149"><a name="p18761104812149"></a><a name="p18761104812149"></a>设置回调函数,定时器到期后触发</p>
178</td>
179</tr>
180</tbody>
181</table>
182### js接口使用说明<a name="section9938411124317"></a>
183
184systemTime模块使用示例:
185
186```
187// 导入模块
188import systemTime from '@ohos.systemTime';
189
190// Promise方式的异步方法设置时间
191var time = 1611081385000;
192systemTime.setTime(time)
193 .then((value) => {
194 console.log(`success to systemTime.setTime: ${value}`);
195 }).catch((err) => {
196 console.error(`failed to systemTime.setTime because ${err.message}`)
197 });
198
199
200// callback方式的异步方法设置时间
201
202var time = 1611081385000;
203systemTime.setTime(time, (err, value) => {
204 if (err) {
205 console.error(`failed to systemTime.setTime because ${err.message}`);
206 return;
207 }
208 console.log(`success to systemTime.setTime: ${value}`);
209 });
210
211```
212
213systemTimer模块使用示例:
214
215```
216// 导入模块
217import systemTimer from '@ohos.systemTimer';
218
219console.log("start")
220var options:TimerOptions{
221 type:TIMER_TYPE_REALTIME,
222 repeat:false,
223 interval:Number.MAX_VALUE/2,
224 persistent:false
225}
226
227console.log("create timer")
228let timerId = systemTimer.Timer(options)
229console.log("start timer")
230let startTimerRes = systemTimer.startTimer(timerId, 100000)
231console.log("stop timer")
232let stopTimerRes = systemTimer.stopTimer(timerId)
233console.log("destroy timer")
234let destroyTimerRes = systemTimer.destroyTimer(timerId)
235console.log('end');
236
237
238```
239
240## 相关仓<a name="section1371113476307"></a>
241
242[Misc软件服务子系统](https://gitee.com/openharmony/docs/blob/master/zh-cn/readme/Misc软件服务子系统.md)
243
244[**time\_time\_service**](https://gitee.com/openharmony/time_time_service/tree/master/)
245
246