简体   繁体   English

asp.net页面中Ajax中的倒数计时器

[英]countdown timer in Ajax in asp.net page

My web page has a timeout of 5 minutes.我的网页有 5 分钟的超时。 I want to show the users the count down in minutes and seconds so I want to show something like :我想向用户显示以分钟和秒为单位的倒计时,所以我想显示如下内容:

4 minutes and 10 seconds left还剩 4 分 10 秒

I tried to implement below code in order to achieve this:我尝试实现以下代码以实现此目的:

<asp:Timer ID="Timer1" runat="server" Interval="5000" ontick="Timer1_Tick">
                        </asp:Timer> 
                    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <span style="border:2px; color:tomato">Time Remaining:<asp:Label ID="Label1" runat="server"></asp:Label></span>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick">
                        </asp:AsyncPostBackTrigger>
                    </Triggers>
                </asp:UpdatePanel>

This is showing the timer, but the time is showing like so:这是显示计时器,但时间显示如下:

在此处输入图片说明

I want to show the timer as minutes and seconds.我想将计时器显示为分钟和秒。 How can I achieve that.我怎样才能做到这一点。 Below is my .cs page code:下面是我的 .cs 页面代码:

protected void Timer1_Tick(object sender, EventArgs e)
{
    Label1.Text = DateTime.Now.Second.ToString();
}

Presently, your code-behind is updating Label1 with the second of the current time.目前,您的代码隐藏正在使用当前时间的秒更新Label1 What you want is how much time is remaining until five minutes after the user first loaded the page.您想要的是在用户第一次加载页面后五分钟还剩多少时间。

Note that if this is simply to display a five-minute countdown, it is certainly simpler and involves far fewer requests to your server to just do so with javascript;请注意,如果这只是为了显示 5 分钟的倒计时,它肯定会更简单,并且只需使用 javascript 即可对您的服务器发出更少的请求; see The simplest possible JavaScript countdown timer?请参阅最简单的 JavaScript 倒数计时器?

That's not precisely what you asked for, so let's take a look at doing it with the asp:Timer and asp:AsyncPostBackTrigger , server-side.这不是正是你要的,所以让我们来看看用做asp:Timerasp:AsyncPostBackTrigger ,服务器端。 To do that, first we'll need to know what time the user loaded the page.为此,首先我们需要知道用户加载页面的时间。 One way you might do that is to set it to the Session (if not already set):您可能会这样做的一种方法是将其设置为会话(如果尚未设置):

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["LoggedInTime"] == null)
        Session["LoggedInTime"] = DateTime.Now;
}

Now, in your postback method:现在,在您的回发方法中:

protected void Timer1_Tick(object sender, EventArgs e)
{
    // Get the time back from Session
    DateTime loggedInTime = Convert.ToDateTime(Session["LoggedInTime"]);

    // Add five minutes to calculate the timeout
    DateTime outaTime = loggedInTime.AddMinutes(5);

    // Subtract the current time to get remaining time
    TimeSpan remainingTime = outaTime.Subtract(DateTime.Now);

    // Now, we're ready to format our Label...
    // First, we probably care if time has already expired
    if (remainingTime.Ticks <= 0)
    {
        Label1.Text = "We're outa time, doc!";
        return;
    }

    // Otherwise, format the Label using our TimeSpan
    Label1.Text = $"{remainingTime.Minutes} minutes and {remainingTime.Seconds} seconds left";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM