AJAX - ASP.NET - 计时器延迟问题
我正在尝试制作一个网络应用程序,您可以在其中看到 Ajax 倒计时器。每当我按下按钮时,倒计时就会回到 30 并继续倒计时。
现在的问题是,每当我按下按钮时,计时器就会持续倒计时一到两秒,之后的大多数时间,计时器都会在 30 上保持很长时间。
网页表单代码:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="geen verbinding"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<br />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">
</asp:Timer>
</form>
代码隐藏:
static int timer = 30;
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = timer.ToString();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
timer--;
}
protected void Button1_Click(object sender, EventArgs e)
{
timer = 30;
}
希望有人知道问题是什么以及是否有解决办法。
提前致谢!
I'm trying to make an webapplication where you see an Ajax countdown timer. Whenever I push a button the countdown should go back to 30 and keep counting down.
Now the problem is whenever I push the button the timer keeps counting down for a second or 2 and most of the time after that the timer keeps standing on 30 for to long.
WebForm code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="geen verbinding"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<br />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">
</asp:Timer>
</form>
Code Behind:
static int timer = 30;
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = timer.ToString();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
timer--;
}
protected void Button1_Click(object sender, EventArgs e)
{
timer = 30;
}
Hope somebody knows what the problem is and if there is anyway to fix this.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不完全在客户端实现计时器?你能解释一下为什么它必须是回调吗?它需要在服务器上做什么?
在正文标签中...
以及要显示的地方
您只需添加一个按钮,调用resetTimer()。我把它留给你
Why don't you implement the Timer entirely on the ClientSide? Can you explain why it has to be a callback? What does it need to do on the Server?
And in the body tag...
And somewhere to display
You justy have to add a button chich calls resetTimer(). I'll leave that to you