View Code
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head runat="server"> 4 <title>js倒计时</title> 5 <style type="text/css"> 6 .fontmark 7 { 8 color: Red; 9 } 10 </style> 11 <script type="text/javascript"> 12 var SurplusTime = { 13 //全局时间 14 Time: { 15 Day: 0, 16 Hour: 0, 17 Minute: 0, 18 Second: 0 19 }, 20 //显示倒计时方法 21 ShowContent: function () { 22 var addtime = function (time) { 23 if (time < 10) time = "0" + time; 24 return " <span class='fontmark'>" + time + "</span>"; 25 } 26 var result = "一天时间倒计时:"; 27 result += addtime(SurplusTime.Time.Day) + "天"; 28 result += addtime(SurplusTime.Time.Hour) + "时"; 29 result += addtime(SurplusTime.Time.Minute) + "分"; 30 result += addtime(SurplusTime.Time.Second) + "秒"; 31 document.getElementById("divTime").innerHTML = result; 32 }, 33 //剩余时间(毫秒) 34 Surplus: 0, 35 //计时器 36 timerSurplus: null 37 }; 38 //获取剩余时间(毫秒数),ajax请求后台 39 function GetSurplusTime() { 40 var xmlhttp; 41 if (window.ActiveXObject) xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 42 else if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest(); 43 xmlhttp.onreadystatechange = function () { 44 if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 45 SurplusTime.Surplus = parseInt(xmlhttp.responseText); 46 CountdownTime(); 47 } 48 } 49 xmlhttp.open("post", "HGetTime.ashx", true); 50 xmlhttp.send(null); 51 } 52 //倒计时 53 function CountdownTime() { 54 var surplus_time = SurplusTime.Surplus; 55 if (surplus_time > 0) { 56 SurplusTime.Time.Day = Math.floor(surplus_time / 86400000) 57 surplus_time -= SurplusTime.Time.Day * 86400000; 58 SurplusTime.Time.Hour = Math.floor(surplus_time / 3600000) 59 surplus_time -= SurplusTime.Time.Hour * 3600000; 60 SurplusTime.Time.Minute = Math.floor(surplus_time / 60000) 61 surplus_time -= SurplusTime.Time.Minute * 60000; 62 SurplusTime.Time.Second = Math.floor(surplus_time / 1000); 63 SurplusTime.Surplus -= 1000; 64 SurplusTime.timerSurplus = setTimeout("CountdownTime()", 1000); 65 } 66 else if (SurplusTime.timerSurplus) clearTimeout(SurplusTime.timerSurplus); 67 SurplusTime.ShowContent(); 68 } 69 //页面加载 70 window.onload = GetSurplusTime; 71 </script> 72 </head> 73 <body> 74 <form id="form1" runat="server"> 75 <div id="divTime"> 76 </div> 77 </form> 78 </body> 79 </html>
Ashx处理:
1 public void ProcessRequest(HttpContext context) 2 { 3 DateTime BeginTime = DateTime.Now; 4 DateTime EndTime = DateTime.Now.AddDays(1); 5 context.Response.Write((EndTime - BeginTime).TotalMilliseconds); 6 }