[JavaScript] A Dynamic Time synchronized to the local time, javascript dynamic

Source: Internet
Author: User

[JavaScript] A Dynamic Time synchronized to the local time, javascript dynamic

This example is very simple. After understanding 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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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.


Javascript: How does a webpage display a dynamic time in seconds? How does the Code write?

Save the following code as an HTML file
<Body>
<Span id = "time"> </span>
</Body>
<Script type = "text/javascript">
Function showtime (){
Var str, time;
Time = new Date ();
Str = "Welcome, now" + time. getFullYear () + "year" + (time. getMonth () + 1) + "month" + time. getDate () + "day" + time. getHours () + ":" + time. getMinutes () + ":" + time. getSeconds ();
Document. getElementById ("time"). innerHTML = str;
}
SetInterval ("showtime ()", 1000 );
</Script>

How to synchronize local time in html?

It's easy to use javascript code to get the current time and then call it once.

<Html>
<Head>
<Title> untitled document </title>
</Head>

<Script type = "text/javascript" language = "javascript">

Function shijian (){
Var myDate = new Date ();
Var mytime = myDate. toLocaleTimeString (); // obtain the current time
Document. getElementById ("av"). innerHTML = mytime;
}
SetInterval ('shijian () ', 1000 );
</Script>
<Body>
<Div id = "av" style = "width: 100px"> </div>
</Body>
</Html>
 

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.