在JSP页面上显示服务器时间 [英] Displaying server time on jsp page
问题描述
我在 jsp
页面上显示服务器时间
,为了显示正确的时间,我必须调用 .jsp
页刷新显示的时间,这会导致许多请求
到服务器
。
I am displaying server time
on a jsp
page, and in order to display correct time, I have to call that jsp
page to refresh the displayed time which leads to many requests
to the server
.
有人可以建议我在 jsp
页面而没有频繁发送请求
?
Can anyone suggest any improvement on the way I'm displaying server time on jsp
page without sending request
frequently?
function displayserver(){
$.post("DisplayServerTime.jsp","",function(data,status, req){
$("#DisplayTimeSection").text(req.responseText.trim());
});
}
$(function(){
setInterval(
function(){
displayserver();
}
, 30000);
});
推荐答案
获取首次加载页面时的服务器时间,然后找出客户端时间与服务器时间之间的时差。
Get the server time when you first load the page, then find out the time difference between the client's time and the server's time.
第二,循环运行一个函数(使用setInterval())。在每次调用时,获取客户端时间并添加您在第一步中获得的时差,然后将其显示。
Second, run recurrently a function (using setInterval()). On each invocation get the client time and add the time difference you got at the first step, then display it.
您将需要运行两次类似的interval函数每秒以确保您不会跳过秒。
You'll need to run the interval function something like twice per second to be sure you don't skip seconds.
这篇关于在JSP页面上显示服务器时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!