JavaScript implementation of dynamic time display synchronized with local time _javascript tips

Source: Internet
Author: User
Tags local time setinterval

The example of this article describes the dynamic time display method of JavaScript implementation synchronized with local time. Share to everyone for your reference. The specific analysis is as follows:

The dynamic display of time is a simple example of what a few lines of code can do after JavaScript,

But for some people who have not touched JavaScript, almost a big project, and then the online search for code, and then in the vast number of HTML code to seek, and ultimately not the essentials.
I. Basic objectives


Implement a Web page text time with the client (on the browser's machine), using the shortest code.


second, the production process

Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>jsclock</title>
<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>

1. When a Date object uses a constructor that has no parameters, it returns the time of the client, and the toLocaleString () method converts the time to the local display time format, if only the ToString () method converts the time to a time string in English. At the same time, realize that the tolocaletimestring () method does not exist in person, please do not toss. If you are not satisfied with the timing of the system's own methods, use the various getday (), getmonth (), getFullYear () methods to construct the string. Do not show again.

2. InnerHTML corresponds to all elements under the ID clock, document.getElementById ("clock"). InnerHTML = time; <span id= "Clock" > </span> Two the contents of the tag are changed to the time string

3. The key to this JavaScript is setinterval ("clock ()", 1000); function, meaning every 1000 milliseconds, that is, every 1 seconds, the clock () function is executed once. That is, every second, the contents of the <span id= "clock" ></span> two tags into a time string are updated once.

I hope this article will help you with your JavaScript programming.

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.