一、页面部分代码

     <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CountDown.aspx.cs" Inherits="CountDown" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
      <script type="text/javascript" language=javascript>
  
          var url;
          var xmlHttp;
        function createXMLHttpRequest()
        {
            if(window.ActiveXObject)
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            else if(window.XMLHttpRequest)
            {
                xmlHttp = new XMLHttpRequest();
            }
        }  
  
        //刷新
        function refurbish()
        {
           var url="GetTime.ashx";
           createXMLHttpRequest();
           xmlHttp.open("POST",url,true);
           xmlHttp.onreadystatechange=Olympiad;
           xmlHttp.send(null);
           setTimeout("refurbish('"+url+"')",1000);
        }  
 
        //获取2008奥运会倒计时的时间
       function Olympiad()
       {  
          
             if(xmlHttp.readyState==4)
            {
                if(xmlHttp.status==200)
                {
                    document.getElementById("show").innerHTML=xmlHttp.responseText;
                }
            }         
          
       }   

 </script>
</head>
<body onload="refurbish()">
    <h3 align="center">奥运倒计时</h3>
    <form > </div>
    </form>
</body>
</html>

二、AJAX代码部分

     <%@ WebHandler Language="C#" Class="GetTime" %>

using System;
using System.Web;

public class GetTime : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";

        //当前时间
        DateTime date=System.DateTime.Now;
        
        //奥运开幕时间
        DateTime date2 = new DateTime(2008,8,8,19,0,0);

        //获取两个时间之间的间隔   
        TimeSpan ts = date2.Subtract(date)

        String message = "今天距2008奥运会开幕还有:<font color='darkred'>"+ts.Days.ToString()+"</font>天&nbsp;";
        message += "<font color='darkred'>"+((ts.Hours<10)?("0"+ts.Hours.ToString()):ts.Hours.ToString())+"</font>小时&nbsp;";
        message += "<font color='darkred'>" + ((ts.Minutes < 10) ? ("0" + ts.Minutes.ToString()) : ts.Minutes.ToString()) + "</font>分&nbsp;";
        message += "<font color='darkred'>" + ((ts.Seconds < 10) ? ("0" + ts.Seconds.ToString()) : ts.Seconds.ToString()) + "</font>秒"

        context.Response.Write(message);
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/pjxv70001/archive/2007/09/17/1788877.aspx

相关文章:

  • 2021-07-05
  • 2021-10-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-04
  • 2021-06-05
  • 2021-06-16
相关资源
相似解决方案