var oDiv = document.createElement('div');
oDiv.id = 'div1';
document.body.appendChild(oDiv);
document.getElementById('div1').style.position="fixed";
document.getElementById('div1').style.left="0";
document.getElementById('div1').style.top="180px";
document.getElementById('div1').style['z-index']="200";
document.getElementById('div1').style.background="#38f";
document.getElementById('div1').style.color="#fff";
var time,nt,add1s;
ajax();
function ajax(){
var xhr = null;
if(window.XMLHttpRequest){
xhr = new window.XMLHttpRequest();
}else{ // ie
xhr = new ActiveObject("Microsoft")
}
// 通过get的方式请求当前文件
xhr.open("get","/");
xhr.send(null);
// 监听请求状态变化
xhr.onreadystatechange = function(){
time = null,
curDate = null;
if(xhr.readyState===2){
// 获取响应头里的时间戳
time = xhr.getResponseHeader("Date");
aa(time)//第一次执行的时候正常
setInterval("aa(time)",1000)//执行setInterval,就变成1970-1-1 8:00:00
}
}
}
function aa(time){
add1s=new Date(time).getTime()+1000;
nt=new Date();
nt.setTime(add1s);//设置新时间比旧时间多一秒
time=nt.setTime(add1s);//把新设置的时间赋给变量time,目的是在下次调用时,在多一秒的基础上继续加1秒
document.getElementById('div1').innerHTML="服务器时间:"+nt.getFullYear()+"-"+(nt.getMonth()+1)+"-"+nt.getDate()+" "+nt.getHours()+":"+nt.getMinutes()+":"+nt.getSeconds();
console.log("时间"+new Date(time))
}
执行setInterval("aa(time)",1000)的时候就不正常了,是为什么呢?
setInterval("aa(time)",1000)
改成