简体   繁体   English

ASP.NET/AJAX-计时器无法正常工作

[英]ASP.NET/AJAX - Timer not working correct

I'm trying to make an webapplication where you see an Ajax countdown timer. 我正在尝试制作一个Web应用程序,在其中您可以看到一个Ajax倒数计时器。 Whenever I push a button the countdown should go back to 30 and keep counting down. 每当我按下按钮时,倒数应回到30并继续倒数。

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. 现在的问题是,每当我按下按钮时,计时器就会持续倒数一秒或2秒,并且在大多数情况下,计时器会一直保持30秒钟的时间。

WebForm code: WebForm代码:

<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! 提前致谢!

since timer is processing the page asynchronously, and the button click event takes time for processing on the server, the timer event still fires in between the button click event and hence the timer keeps counting back for a second or two. 由于计时器正在异步处理页面,并且按钮单击事件需要花费一些时间来在服务器上进行处理,因此计时器事件仍然在按钮单击事件之间触发,因此计时器会持续倒数一两秒。 Use Java script to set the label to 30 on client side as soon as the reset button is clicked. 单击重置按钮后,立即使用Java脚本在客户端将标签设置为30。 Upon timer click event, decrement the label value (not the timer) and assign to the label the new value. 发生计时器单击事件时,递减标签值(而不是计时器),并将新值分配给标签。 No need for timer int variable. 不需要计时器int变量。 Also on page load, assign the label value only if the page is not postback (ie IsPostback is false) as we want to load label value only on first time the page is rendered. 同样在页面加载时,仅当页面未回发(即IsPostback为false)时才分配标签值,因为我们仅希望在第一次呈现页面时加载标签值。 Rest of the time, the timer click event will assign the value. 其余时间,计时器单击事件将分配值。

The problem was that Visual Studio hosted it on localhost. 问题是Visual Studio在本地主机上托管了它。 If you use ip-adress 127.0.0.1 instead of local host within the URL it worked fast. 如果您在网址中使用ip-adress 127.0.0.1代替本地主机,则它可以快速运行。 I guess this won't be a problem on faster machines, sadfully I had none at that time. 我猜这在较快的机器上不会有问题,可惜当时我没有。

EDIT: Adding a bounty on this question was a mistake, sorry about that. 编辑:在这个问题上添加赏金是一个错误,对此感到抱歉。

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

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