.Net和javascript实现服务器时间电子时钟

摘要 : . Net and javascript to achieve server time electronic clock

  <script language="javascript">
 
       var intHours   = 0;
         var intMinutes = 0;
         var intSeconds = 0;
 function clock()
 {
         //分别取得传入的时、分、秒的数值



         //格式化待输出的样式
         if (intHours < 10) {
                 hours = "0"+intHours+":";
         } else {
                 hours = intHours+":";
         }


         if (intMinutes < 10) {
                 minutes = "0"+intMinutes+":";
         } else {
                 minutes = intMinutes+":";
         }
         if (intSeconds < 10) {
                 seconds = "0"+intSeconds+" ";
         } else {
                 seconds = intSeconds+" ";
         }


         intSeconds += 1;    //秒自增1 不论在哪里 每秒时间都是相同的
         if (intSeconds>=60)
         {
                 intSeconds = 0;
                 intMinutes +=1;
         }


         if (intMinutes>=60)
         {
                 intHours   += 1;
                 intSeconds  = 0;
                 intMinutes  = 0
         }


         if (intHours>=24)
         {
                 intHours   = 0;
                 intSeconds = 0;
                 intMinutes = 0;
         }


         timeString = hours+minutes+seconds;


         //定位到文档中输出
         document.getElementById('clock').innerHTML = timeString;
         //显示间隔
         window.setTimeout("clock();", 1000);
 }
function SetClock(hour,minute,second)
{
    intHours   = hour;
    intMinutes = minute;
    intSeconds = second;
} </script>

        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:Timer ID="Timer1" runat="server" Interval="120000" OnTick="Timer1_Tick">
        </asp:Timer>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
            </Triggers>
        </asp:UpdatePanel>

        <script type="text/javascript">SetClock(<%=DateTime.Now.Hour %>,<%=DateTime.Now.Minute %>,<%=DateTime.Now.Second %>);clock();</script>


    protected void Timer1_Tick(object sender, EventArgs e)
    {
      
        ScriptManager.RegisterStartupScript(UpdatePanel1, typeof(UpdatePanel), "test", "SetClock(" + DateTime.Now.Hour + "," + DateTime.Now.Minute + "," + DateTime.Now.Second + ");", true);
       
    }
上一篇 :Vml+Dhtml页面特效 淘气鬼
下一篇 :忽略大小写获取对象的属性