How to display the clock with js

Source: Internet
Author: User

This section is a bit interested in js. It is interesting to have a few small js experiments. One of them is to control the display of the clock. If you are interested, you can study it! Let's take a look at the following code:

 

First, declare two variables in the js Code: timerID and timerRunning. You can see that in the showTimes method, assign timerId: timerID = setTimeout ("showTimes ()", 1000 ); indicates that the Browser executes the showTimes method 1000 milliseconds after receiving the setTimeout command. to cancel the timeout setting, it calls stopClock (). In this method, there is a clearTimeout (timerID ); you can cancel the time-out setting. If the time-out setting has not occurred, the showTimes method will not be called. The setTimeout () method is very interesting, if you are interested, you can compare setInterval ("showTime ()", 1000) to see the execution results of the two methods. As the name suggests, timerRunning indicates whether the time is displayed, it mainly serves stopClock () and showTimes.
In addition, we can see that the showTimes method in js contains a document. clock. face. value = timerVal; document contains the entire html document. In html, you can see that the name attribute of form is clock, and the input element of form contains text of type = "text, its name attribute is face, so that you can find the text in the html page and set its value to the time we get. This will display the time in the corresponding text box on the page, to obtain the time, you can declare a time object var now = new Date (); then call its getYear () and getMonth () to obtain the time. After setting, call this method during html loading, that is, <body onLoad = "startClock ()"> in this way, the startClock method is automatically called when the html page is loaded, so that the time is displayed in the page text.
Js is very interesting. If you are interested, you can study it together!


This article is from the "HDDevTeam" blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.