How to display the server time synchronously on the client:
1. Use Ajax asynchronous commit to get the time, and then partial refresh the page.
2. Obtain the server time and accumulate the time on the local (client.
The first method obviously increases the server load, but the time is accurate. (The intermediate network speed is calculated separately)
In the second example, only one time is obtained at a time, saving the server overhead.
This time I used the second method to implement this function, hoping to help the learners.
The server time code obtained on the client is as follows:
<%
Date = new date ();
%>
Use the JS function to accumulate the time.
<Script language = "JavaScript">
VaR hours, minutes, seconds, XFile;
VaR inthours, intminutes, intseconds;
VaR today = new date ();
Today. settime (<% = date. gettime () %>); // assign the server time to the JS Function
Function time_callback (){
Inthours = today. gethours ();
Intminutes = today. getminutes ();
Intseconds = today. getseconds ();
If (inthours = 0 ){
Hours = "12 :";
XFile = "Midnight ";
} Else if (inthours <12 ){
Hours = inthours + ":";
XFile = "Morning ";
} Else if (inthours = 12 ){
Hours = "12 :";
XFile = "Noon ";
} Else {
Inthours = inthours-12
Hours = inthours + ":";
XFile = "Afternoon ";
}
If (intminutes <10 ){
Minutes = "0" + intminutes + ":";
} Else {
Minutes = intminutes + ":";
}
If (intseconds <10 ){
Seconds = "0" + intseconds + "";
} Else {
Seconds = intseconds + "";
}
Timestring = XFile + hours + minutes + seconds;
Document. getelementbyid ("time_view"). innerhtml = "& nbsp;" + timestring;
Today. settime (today. gettime () + 1000 );
}
Setinterval ("time_callback ()", 1000); // calls back this function every 1000 milliseconds
</SCRIPT>