BS系统,要让原来的JS取本地时间的时钟变成取服务器的时间,这里的想法是取出服务器的时间与本地的时间相减(转为毫秒),然后每次秒变时都把这个差值算上。
对于局域网的应用,一般不用考虑取值的时间,如果是网络不稳定可以考虑把从服务器取值的时间也加进去,这样更为精确。
对于局域网的应用,一般不用考虑取值的时间,如果是网络不稳定可以考虑把从服务器取值的时间也加进去,这样更为精确。
//用来存放差值
var differentMillisec = 0;
function init() {
//取时间差值
getServerDate();
//取得显示时间
showtime();
}
//取得显示时间
function showtime(){
now = new Date();
now.setTime(differentMillisec + now.getTime());
var str = now.getYear() + "年";
var temp = now.getMonth() + 1;
if (temp < 10) str += "0";
str += temp + "月";
temp = now.getDate();
if (temp < 10) str += "0";
str += temp + "日";
var today = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
str += " " + today[now.getDay()] + " ";
temp = now.getHours();
if (temp < 10) str += "0";
str += temp + ":";
temp = now.getMinutes();
if (temp < 10) str += "0";
str += temp + ":";
temp = now.getSeconds();
if (temp < 10) str += "0";
str += temp;
document.getElementById("Head1Right_Time").innerHTML = str;
ctroltime=setTimeout("showtime()", 1000);
}
//从服务器取时间,用的buffalo取
function getServerDate() {
begin = new Date();
millisecbeg = begin.getTime();
try {
var buffalo = new Buffalo(endPointTop);
buffalo.remoteCall("desktopService.getServerDate", [], function(reply) {
var serverMillisec = reply.getResult();
end = new Date();
millisecend = end.getTime();
differentMillisec = serverMillisec - new Date() + (millisecend - millisecbeg)/2;
});
} catch (ex) {
}
}
//获得服务器当前时间
public String getServerDate() {
return String.valueOf(System.currentTimeMillis());
}