Js-implemented time and date code instances that can be automatically refreshed, and js-automatically refreshed
Example of the time and date code implemented by js that can be automatically refreshed:
The time and date effects that can be automatically refreshed are used on many web pages, because they make the pages more dynamic and user-friendly, and users can view the page time in real time, the following describes how to achieve this through code examples.
The Code is as follows:
<! DOCTYPE html>
The above code implements automatic refresh of time and date. The following describes the implementation process.
I. Code comments:
1. function getTime () {}, which obtains the current time and date.
2. var odate = new Date (), create the object of the current time and Date.
3. var date = odate. toLocaleDateString (), you can obtain the local year, month, and day.
4. var hours = odate. getHours (), get the hour.
5. var minutes = odate. getMinutes (), get minutes.
6. var seconds = odate. getSeconds (), get seconds.
7. $ ("# showDate" 2.16.html (date + "" + (hours> 9? Hours: "0" + hours) + ":" + (minutes> 9? Minutes: "0" + minutes) + ":" + (seconds> 9? Seconds: "0" + seconds) to write the current time and date to the specified element. Of course, this is div, this is to use the ternary operator to add 0 to the front when the hour, minute, and second are single digits.
8. $ (function () {setInterval ("getTime ();", 1000) ;}), the specified function is executed every second.
Ii. Related reading:
1. For more information about time objects, see the Date object section of ECMAScript.
2. For more information about ternary operators, see section 1.
3. For details about setInterval () functions, refer to setInterval () function usage.
The original address is: http://www.softwhy.com/forum.php? Mod = viewthread & tid = 14636.
For more information, see: http://www.softwhy.com/jquery/