js取服务器时间显示时钟

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());
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值