JavaScript implements the Dynamic Time Display Method synchronized to the local time, javascript dynamic

Source: Internet
Author: User

JavaScript implements the Dynamic Time Display Method synchronized to the local time, javascript dynamic

This example describes how to display the Dynamic Time of JavaScript synchronized to the local time. Share it with you for your reference. The specific analysis is as follows:

The example of dynamically displaying time is very simple. After learning about JavaScript, several lines of code can be done,

But for some people who have never touched JavaScript, it looks like a huge project, and then they are struggling to find code on the Internet. Then they cannot find the code in the vast html code, and ultimately they cannot get the essentials.
I. Basic Objectives


Implement the webpage text time that is accompanied by the client (on the browser's machine), and use the shortest code.


Ii. Production Process
Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> jsclock </title>
</Head>
<Body>
<Script type = "text/javascript">
Function clock (){
Var time = new Date (). toLocaleString ();
Document. getElementById ("clock"). innerHTML = time;
}
SetInterval ("clock ()", 1000 );
</Script>
<Span id = "clock"> </span>
</Body>
</Html>

1. if the Date object uses a constructor without parameters, the client's time will be returned. The toLocaleString () method is to convert the time to the time format to display the time at cost. If it is only toString () the time is converted into a time string written in English. At the same time, I personally discovered that the toLocaleTimeString () method does not exist. Please do not try it. If you are not satisfied with the time converted by the built-in methods, use various methods such as getDay (), getMonth (), and getFullYear () to construct the string. Do not show again.

2. innerHTML is equivalent to all the elements under the id of clock, document. getElementById ("clock "). innerHTML = time; the meaning of a sentence is to convert the content in the <span id = "clock"> </span> label into the content of the time string.

3. The key to this JavaScript code is setInterval ("clock ()", 1000). The function, meaning that the clock () function is executed once every 1000 milliseconds, that is, every 1 second. That is, the content in the <span id = "clock"> </span> label is updated every second to the time string.

I hope this article will help you design javascript programs.

Related Article

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.