Ale*_*yev 4 javascript asp.net asp.net-ajax
你能推荐我一种在ASP.NET页面上放置一个coundown计时器的方法吗?
现在我使用这段代码:
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>
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
但这是交通昂贵.我更喜欢纯客户端方法.是否可以在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>
Run Code Online (Sandbox Code Playgroud)
真的很简单.就像旧的简单HTML与JavaScript一样.
| 归档时间: |
|
| 查看次数: |
44447 次 |
| 最近记录: |