"JavaScript" a dynamic time synchronized to local time

Source: Internet
Author: User
Tags setinterval

This is a very simple example of what a few lines of code can do after JavaScript,

But for some people who have not been in touch with JavaScript, almost a big project, and then on the Internet to search for code, and then in the vast HTML code to seek, and ultimately do not have the essentials.


I. BASIC OBJECTIVES


Implement a Web page text time that accompanies the client (on the browser machine), using the shortest code.


Second, the production process

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

1.Date Object If you use a constructor that has no parameters, the time is returned to the client, and the toLocaleString () method is to convert the time to a local display time format, and if only the ToString () method only converts the time into a time string of English notation. At the same time, personally realize the Discovery toLocaleTimeString () method is not present, please do not toss. If you are not satisfied with the time the system comes in, use the various methods of Getday (), GetMonth (), getFullYear () to construct the string. Not to show again.


The 2.innerHTML corresponds to all the elements under the clock, document.getElementById ("clock"). InnerHTML = time; The meaning of a sentence <span id= "Clock" > The contents of the </span> two tag changed to the time string


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

"JavaScript" a dynamic time synchronized to local time

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.