JS implementation of a time-date code instance that can be automatically refreshed

Source: Internet
Author: User

JS implementation can automatically refresh the time-date code instance:
can automatically refresh the time date effect in a lot of pages are in use, because can make the page more dynamic, but also more user-friendly, users can view the time of the page in real-time, the following is a code example to explain how to achieve this effect.
The code is as follows:

<!DOCTYPE HTML> <HTML> <Head> <MetaCharSet= "Utf-8"> <Metaname= "Author"content= "http://www.softwhy.com/" /> <title>Ant Tribe</title> <Scripttype= "Text/javascript"src= "Mytest/jquery/jquery-1.8.3.js"></Script><Scripttype= "Text/javascript"> functionGetTime () {varodate=NewDate (); varDate=odate.tolocaledatestring (); varhours=odate.gethours (); varminutes=odate.getminutes (); varseconds=odate.getseconds (); $("#showDate"). HTML (Date+" "+(Hours>9?Hours:"0"+hours)+":"  +(Minutes>9?minutes:"0"+minutes)+":"  +(Seconds>9?seconds:"0"+seconds));} $(function() {setinterval ("getTime ();", +);}) </Script> </Head> <Body> <DivID= "Showdate"></Div></Body> </HTML>

The above code realizes the automatic refresh effect of the implementation time date, and the following describes its implementation process.
A. Code Comment:
1.function getTime () {}, this function can get the current time date.
2.var odate=new Date (), which creates the current time-of-day object.
3.var date=odate.tolocaledatestring (), you can get the local month day.
4.var hours=odate.gethours (), gets the hour.
5.var minutes=odate.getminutes (), get minutes.
6.var seconds=odate.getseconds (), gets the seconds.
7.$ ("#showDate"). HTML (date+ "" + (Hours>9?hours: "0" +hours) + ":" + (minutes>9?minutes: "0" +minutes) + ":" + (seconds >9?seconds: "0" +seconds), writes the current time date to the specified element, which is, of course, the Div, which is used to implement the ternary operator in order to achieve the front plus 0 when the hour, minute, and second are single digits.
8.$ (function () {setinterval ("getTime ();", 1000);}) executes the specified functions once per second.
Two. Related reading:
1. For a Time object, refer to the ECMAScript Date Object section.
2. Ternary operators can refer to the chapter on the use of ternary operators.
The 3.setInterval () function can be found in the setinterval () Function usage section.

The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=14636

For more information, refer to: http://www.softwhy.com/jquery/

JS implementation of a time-date code instance that can be automatically refreshed

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.