The real-time display time on the webpage not only adds color to the webpage, but also helps the viewer master the current time. In order to improve the website development speed, you can encapsulate the main code in a separate function and call it directly when necessary. For demonstration, I wrote it on the home page for your convenience (1 ): first, write the function clockon () for real-time system time display in JS with only one parameter bgclock, which is used to specify the display for the converted
The marked name has no returned value. When you develop a website, you can save the modified function in the JS file to reuse the Code as follows:
The Code is as follows:
Script
Function clockon (bgclock)
{
Var now = new Date ();
Var year = now. getYear ();
Var month = now. getMonth ();
Var date = now. getDate ();
Var day = now. getDay ();
Var hour = now. getHours ();
Var minu = now. getMinutes ();
Var sec = now. getSeconds ();
Var week;
Month = month + 1;
If (month <10) month = "0" + month;
If (date <10) date = "0" + date;
If (hour <10) hour = "0" + hour;
If (minu <10) minu = "0" + minu;
If (sec <10) sec = "0" + sec;
Var arr_week = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday ");
Week = arr_week [day];
Var time = "";
Time = year + "year" + month + "month" + date + "day" + week + "" + hour + ";" + minu + ";" + sec;
If (document. all)
{
Bgclock. innerHTML = "[" + time + "]"
}
Var timer = setTimeout ("clockon (bgclock)", 200 );
}
Script
(2 ):Called in the onload event and put the p tag code in the place to display:
The Code is as follows: