zoukankan      html  css  js  c++  java
  • Using Timer

    1.Aspx code

      <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:HiddenField ID="FinishTime" runat="server" />
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <br />
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
           
                <ContentTemplate>
                    <asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">
                    </asp:Timer>
                    <asp:Label ID="labTimes" runat="server" Width="155px"></asp:Label>
                </ContentTemplate>
            </asp:UpdatePanel>
       
        </div>
        </form>
    </body>
    </html>

    2.CS Code

     public partial class DemoTimer : System.Web.UI.Page
        {
            private double TotalTime = 0.01;

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    this.FinishTime.Value = DateTime.Now.AddHours(TotalTime).ToString("yyyy/MM/dd HH:mm:ss");
                }
            }

            protected void Timer1_Tick(object sender, EventArgs e)
            {
                DateTime finishTime = DateTime.Parse(this.FinishTime.Value);
                if (DateTime.Now >= finishTime) //如果设置的时间已过
                {
                    this.Timer1.Enabled = false;    //将Timmer置为false
                    this.labTimes.Text = "时间到!";
                }
                else
                {
                    RefreshTime(); //刷新时间
                }

            }
            private void RefreshTime() //刷新时间的方法
            {
                TimeSpan ts = DateTime.Parse(this.FinishTime.Value) - DateTime.Now; //时间差           
                this.labTimes.Text = ts.Hours.ToString().PadLeft(2, '0') + ":" + ts.Minutes.ToString().PadLeft(2, '0') + ":" + ts.Seconds.ToString().PadLeft(2, '0');
            }

        }

  • 相关阅读:
    iOS渠道分包2种模式之包内注入文件分包(iOS13验证签名问题)
    iOS13 新特性简介
    OC 字典dictionaryWithObjectsAndKeys报错
    博客迁移指南
    block内部实现原理(三)
    block内部实现原理(二)
    block内部实现原理(一)
    iOS:记一次Mac OS X 测试版(OS X EL Capitan) APP发布过程
    iOS: El Capitan Beta 下 Xcode6.4 不显示Scheme菜单
    iOS: UIWebView 中不加载图片(即浏览器常见的无图模式)
  • 原文地址:https://www.cnblogs.com/Excellentchen/p/1781402.html
Copyright © 2011-2022 走看看