# How to Develop The Clock app displays the current time through a clock face and numbers. As shown in [Figure 1 Clock display effect](overview-7.md#fig7763172132019), the page consists of two parts: - Clock face area: displays a dynamic analog clock whose hands rotate accurately. - Digital time area: displays the current time in numerals. To build such an app, we can create a page that has a flexible layout with two rows vertically arranged. The development procedure is as follows: 1. Add a root **** to the **.hml** file. Note that each **.hml** file can contain only one root component. The example code is as follows: ```
``` **class="container"** indicates the style used by the component. The **container** is a style class defined in the **index.css** file. ``` .container { flex-direction: column; justify-content: center; align-items: center; width: 100%; height: 100%; } ``` The height and width of the root component **** are set in the style class. Note that the height and width must be explicitly specified \(except for some components, such as ****\). Otherwise, the component may fail to display. In the **container** style class, the **flex-direction** attribute is set to **column**, which means that child components of **** are vertically arranged from top to bottom for implementing the flexible page layout. 2. Implement clock hand rotation using the **** component. The **** component provides a stack container where child components are successively stacked and the latter one overwrites the previous one. Add a stack container to the root component. The example code is as follows: ```
``` **style="transform: rotate\(\{\{ second \* 6 \}\}deg\)** sets the rotation event of a component. **transform** translates, rotates, or scales an element. **rotate** rotates an element. You can set the element to rotate around its x-axis or y-axis. Set attributes, such as the height, width, and position, of the component in the CSS file. The example code is as follows: ``` .stack { flex-direction: column; justify-content: center; align-items: center; width: 100%; height: 50%; } ``` Set attributes, such as the height and width, of the component in the CSS file. The example code is as follows: ``` .clock-bg { width: 80%; height: 80%; object-fit: scale-down; } ``` Set attributes, such as the height and width of the hour, minute, and second hands, of the component in the CSS file. The example code is as follows: ``` .clock-hand { width: 25%; height: 65%; object-fit: contain; } ``` Add a timer in the **index.js** file to update the hour, minute, and second variables in real time so that time can be automatically updated on the app UI. The example code is as follows: ``` export default { timer: undefined, // Define parameters. data: { hour: 0, // Define hours. minute: 0, // Define minutes. second: 0 // Define seconds. }, onInit () { this.updateTime(); this.timer = setInterval(this.updateTime, 1000)// Set the update time of the timer to 1 second. }, updateTime: function () { var nowTime = new Date() this.hour = nowTime.getHours() this.minute = nowTime.getMinutes() this.second = nowTime.getSeconds() if (this.hour < 10) { this.hour = '0' + this.hour } if (this.minute < 10) { this.minute = '0' + this.minute } if (this.second < 10) { this.second = '0' + this.second } }, } ``` 3. Display the current time in digital form under the analog clock. Add the **** component at the end of the root layout. The following example shows the page structure: ``` {{ hour }}:{{ minute }}:{{ second }} ``` **class="digit-clock"** sets the height, width, and font size of the component. The example code is as follows: ``` .digit-clock { font-size: 58px; width: 100%; margin-top: 0px; text-align: center; } ``` 4. Set the style, animation effect, and dynamic data binding for all components. The complete example code is as follows: - **index.hml** ```
{{ hour }}:{{ minute }}:{{ second }}
``` - **index.css** ``` .container { flex-direction: column; justify-content: center; align-items: center; width: 100%; height: 100%; } .stack { flex-direction: column; justify-content: center; align-items: center; width: 100%; height: 50%; } .digit-clock { font-size: 58px; width: 100%; margin-top: 0px; text-align: center; } .clock-bg { width: 80%; height: 80%; object-fit: scale-down; } .clock-hand { width: 25%; height: 65%; object-fit: contain; } ``` - **index.js** A **.js** file is used to implement interaction logic of your app. In the **.js** file of the page, implement the function of periodically obtaining the system time. ``` export default { timer: undefined, data: { hour: 0, minute: 0, second: 0 }, onInit() { this.updateTime() this.timer = setInterval(this.updateTime, 1000) }, updateTime: function () { var nowTime = new Date() this.hour = nowTime.getHours() this.minute = nowTime.getMinutes() this.second = nowTime.getSeconds() if (this.hour < 10) { this.hour = '0' + this.hour } if (this.minute < 10) { this.minute = '0' + this.minute } if (this.second < 10) { this.second = '0' + this.second } }, onDestroy() { clearInterval(this.timer); } } ```