让前端飞Web前端之路Web 前端开发

倒计时模块(获取的是服务器上的时间——适合购物网站的抢购模块)

2019-11-06  本文已影响0人  2o壹9

JS部分

/*ajax请求服务器当前时间*/function servertiems() { 

  varA=null;

  try {

    A=newActiveXObject("Msxml2.XMLHTTP")

  } catch(e) {

    try {

      A=newActiveXObject("Microsoft.XMLHTTP")

    } catch(oc) {

      A=null    }

  }

  if( !A &&typeofXMLHttpRequest != "undefined" ) {

    A=new XMLHttpRequest()

  }

  return A

}

  var NowTime;

  vartm = servertiems(); 

  varstrData="auto="+new Date().toString();

  tm.onreadystatechange = callback_st;

  tm.open("GET","http://您的服务器输出地址",false);//服务器输出格式如:{"time": "2016/4/29 11:24:11"}tm.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

  tm.send(strData);

  function callback_st(){

    if(tm.readyState==4 && tm.status==200){

    varresult = tm.responseText;   

    varjson = eval("(" + result + ")");

    NowTime=json.time;//获取到服务器当前时间   

    }

}vartoday=new Date(NowTime).getTime();varnow =new Date().getTime();vardiff = (now - today);function show_date_timezj(timesxx,xid){

window.setTimeout(function(){show_date_timezj(timesxx,xid)},20);

BirthDay=newDate(timesxx);//设置日期varnow_fun =new Date().getTime();vartodayx=now_fun - diff;

timeold=(BirthDay - now_fun + diff );

sectimeold=timeold/1000;secondsold=Math.floor(sectimeold);

msPerDay=86400000; //*天数e_daysold=timeold/msPerDay;daysold=Math.floor(e_daysold); //*小时e_hrsold=(e_daysold-daysold)*24;

hrsold=Math.floor(e_hrsold);

e_minsold=(e_hrsold-hrsold)*60;

minsold=Math.floor((e_hrsold-hrsold)*60);

e_seconds = (e_minsold-minsold)*60;

seconds=Math.floor((e_minsold-minsold)*60);

ms = e_seconds-seconds;

ms =new String(ms)

ms = ms.substr(2,3);if(daysold>=0){

document.getElementById(xid).innerHTML="距离开始时间还有: "+(daysold<10?'0'+daysold:daysold)+"天"+""+(hrsold<10?'0'+hrsold:hrsold)+"小时"+""+(minsold<10?'0'+minsold:minsold)+"分"+""+(seconds<10?'0'+seconds:seconds)+"秒"+""+ms+"";

}else{

document.getElementById(xid).innerHTML='';

document.getElementById(xid).nextSibling.className='';

}

}

HTML部分

<div><div id="span_k1"></div><a href="" class="no_ac">进入</a><div id="span_k2"></div><a href="" class="no_ac">进入</a><div id="span_k3"></div><a href="" class="no_ac">进入</a></div>

 JS调用

varspank1=newshow_date_timezj("05/06 /2015 19:30:00","span_k1");varspank2=newshow_date_timezj("05/07 /2015 09:00:00","span_k2");varspank3=newshow_date_timezj("05/08 /2015 19:00:00","span_k3");

上一篇下一篇

猜你喜欢

热点阅读