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.