How does JSP synchronously display the server time on the client?

Source: Internet
Author: User

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>

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.