asp.net用戶端同步服務端即時重新整理時間

來源:互聯網
上載者:User


 
//首先加入下列指令碼
<script>...
        //擷取時間
    function Timer(span)
        ...{
                            var tmp = new Date();
            var milsecs=Date.parse(tmp.getMonth()+"-"+tmp.getDay()+"-"+tmp.getFullYear()+" "+document.getElementById(''lbl_Hour'').innerText+":"+document.getElementById(''lbl_Minute'').innerText+":"+document.getElementById(''lbl_Second'').innerText);
            var timer = new Date(milsecs+span);
            var seconds,minutes,hours;
            if(timer.getSeconds()<10)
                seconds = "0"+timer.getSeconds();
            else
                seconds = timer.getSeconds();
            if(timer.getMinutes()<10)
                minutes = "0"+timer.getMinutes();
            else
                minutes = timer.getMinutes();
            if(timer.getHours()<10)
                hours = "0"+timer.getHours();
            else
                hours = timer.getHours();    
            document.getElementById(''lbl_Second'').innerText = seconds;
            document.getElementById(''lbl_Minute'').innerText = minutes;
            document.getElementById(''lbl_Hour'').innerText = hours;
        }
        //得到伺服器時間每隔updatespan分鐘校正一次,每秒更新一次本地時鐘
        function GetServerTime(updatespan)
        ...{    
            var clientspan = 1*1000;
            //更新本地時鐘
            setInterval("Timer("+clientspan+")",clientspan);
            //同步伺服器時鐘 注意''SendErrorTaskModify.aspx''是你當前頁的名稱
            setInterval("window.location.href=''SendErrorTaskModify.aspx''",updatespan);
            
        }
        //-->
    </script>
                     //下面這個方法也可以移到aspx.cs後台檔案中
    <script language="C#" runat="server">...
        
        string GetTime(object day)
        ...{
            if(day.ToString()=="")
                return "";
            else
            ...{
                return(DateTime.Parse(day.ToString()).ToShortTimeString());            
            }
        
        }
    </script>

//接著在body 中添加如下方法
<body onload="GetServerTime(15*60*1000)">

//在介面中添加如下時間的label
 <tr>
  <td colspan="4">
                  <asp:Label ID="lbl_Time" runat="server" Font-Size="X-Small" ForeColor="blue"></asp:Label>
                  <asp:Label ID="lbl_Hour" runat="server" Font-Size="X-Small" ForeColor="blue">Label</asp:Label>:
                  <asp:Label ID="lbl_Minute" runat="server" Font-Size="X-Small" ForeColor="blue">Label</asp:Label>:
                  <asp:Label ID="lbl_Second" runat="server" Font-Size="X-Small" ForeColor="blue">Label</asp:Label>
   </td>
   </tr>
//在aspx.cs後台代碼中PageLoad(...)中添加 這個非常重要
    lbl_Hour.Text = DateTime.Now.Hour.ToString();
        lbl_Minute.Text = DateTime.Now.Minute.ToString();
        lbl_Second.Text = DateTime.Now.Second.ToString();
<

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.