ASP.NET页上的倒数计时器 [英] Countdown timer on ASP.NET page

查看:91
本文介绍了ASP.NET页上的倒数计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能推荐我一种在ASP.NET页面上放置计时器的方法吗?

Could you recommend me a way to place a coundown timer on ASP.NET page?

现在我使用此代码:

Default.aspx

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
    <asp:Label ID="Label1" runat="server">60</asp:Label>
    <asp:Timer ID="Timer1" runat="server" Interval="1000" 
        ontick="Timer1_Tick">
    </asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>

Default.aspx.cs

protected void Timer1_Tick(object sender, EventArgs e)
{
    int seconds = int.Parse(Label1.Text);
    if (seconds > 0)
        Label1.Text = (seconds - 1).ToString();
    else
        Timer1.Enabled = false;
}

但这是交通昂贵的东西.我更喜欢纯客户端方法.在ASP.NET中可以吗?

But it is traffic expensive. I would prefer pure client-side method. Is it possible in ASP.NET?

推荐答案

好的,最后我结束了

<span id="timerLabel" runat="server"></span>

<script type="text/javascript">

    function countdown() 
    {
        seconds = document.getElementById("timerLabel").innerHTML;
        if (seconds > 0) 
        {
            document.getElementById("timerLabel").innerHTML = seconds - 1;
            setTimeout("countdown()", 1000);
        }
    }

    setTimeout("countdown()", 1000);

</script>

真的很简单.就像使用JavaScript的旧的纯HTML一样.

Really simple. Like old good plain HTML with JavaScript.

这篇关于ASP.NET页上的倒数计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆