利用ajax获取服务器时间,并显示

本文介绍了一种使用JavaScript和XMLHttpRequest获取服务器当前时间的方法,并通过调整本地时间与时区差异来实现实时显示服务器时间的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
<html>
<head>
<title>获取服务器端时间并不停网页在显示上</title>
</head>
<body>
<p><ahref="http://www.studynote.cn">学习笔记</a>:<ahref="http://www.studynote.cn">http://www.studynote.cn</a></p>
<spanid="spnTime"></span>
<scriptlanguage="javascript">
functioncreateXMLHttpRequest(){
varrequest=false;
if(window.XMLHttpRequest){
request=newXMLHttpRequest();
if(request.overrideMimeType){
request.overrideMimeType('text/xml');
}
}elseif(window.ActiveXObject){
varversions=['Microsoft.XMLHTTP','MSXML.XMLHTTP','Microsoft.XMLHTTP','Msxml2.XMLHTTP.7.0','Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP'];
for(vari=0;i<versions.length;i++){
try{
request=newActiveXObject(versions[i]);
if(request){
returnrequest;
}
}catch(e){
//alert(e.message);
}
}
}
returnrequest;
}
varhttp=createXMLHttpRequest();
http.open("HEAD",".",false);
http.send(null);
varcurDate=newDate;
varoffsetTime=curDate-Date.parse(http.getResponseHeader("Date"));
setInterval(function()
{
curDate.setTime(newDate-offsetTime);
document.getElementById("spnTime").innerHTML=curDate.toLocaleString();
},1000);
</script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值