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

作者: Robin 分类: Asp.net 发布时间: 2010-05-07 12:01

  <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);
       
    }

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注

标签云