JavaScript倒计时获取服务器时间

//获取服务器时间

functiongetSevertime(){

varxmlHttp=newXMLHttpRequest();

if(!xmlHttp){

xmlHttp=newActiveXObject("Microsoft.XMLHTTP");

}

xmlHttp.open("HEAD",location.href,false);

xmlHttp.send();

varsevertime=newDate(xmlHttp.getResponseHeader("Date"));

returnsevertime;

}

//换奖活动兑奖倒计时

varclockTime=null;

varclockRuning=false;

functionsetClock(){

//varnow=newDate();

varnow=getSevertime();

varendTime=newDate('2016/01/1610:00:00');

vart=endTime.getTime()-now.getTime();

if(t<0)clearInterval(clockTime);

vard=Math.floor(t/1000/60/60/24);

varh=Math.floor(t/1000/60/60%24);

varm=Math.floor(t/1000/60%60);

vars=Math.floor(t/1000%60);

var_html='';

if(d>0)_html+='<spanclass="muted">'+d+'</span><spanclass="span">天</span>';

if(h>=10)_html+='<spanclass="muted">'+h.toString().slice(0,1)+'</span><spanclass="muted">'+h.toString().slice(1,2)+'</span><spanclass="span2">:</span>';

if(h<10)_html+='<spanclass="muted">0</span><spanclass="muted">'+h+'</span><spanclass="span2">:</span>';

if(m>=10)_html+='<spanclass="muted">'+m.toString().substring(0,1)+'</span><spanclass="muted">'+m.toString().substring(1,2)+'</span>';

if(m<10)_html+='<spanclass="muted">0</span><spanclass="muted">'+m+'</span>';

if(getId('clockTime')){

getId('clockTime').innerHTML=_html;

}

}

setClock();//初始化

varclockTime=setInterval(setClock,60000);

相关推荐