1

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!

6
  • there will be some amount of delay due to the fact that you are doing a partial postback to the server.
    – derek
    Apr 22, 2010 at 16:50
  • But there must be something to do against that, or incalculate it or something.
    – Julian
    Apr 22, 2010 at 16:51
  • Your static int timer is split among everyone accessing your page. You should have one timer per session. (if you have more than one client accessing the page) Apr 22, 2010 at 16:57
  • Thanks for that info Mikael, but atm it stays a 1 session timer. I just need this problem fixed so it works right.
    – Julian
    Apr 22, 2010 at 17:07
  • 1
    Are you married to doing this server-side with an updatepanel? These kinds of ajax-caused delays and hiccups could be minimized by implementing the timer directly on the client using just javascript. Your options to fix this are pretty limited using the built-in asp.net controls.
    – Rudism
    Apr 26, 2010 at 18:56

2 Answers 2

1

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. 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. Also on page load, assign the label value only if the page is not postback (i.e. IsPostback is false) as we want to load label value only on first time the page is rendered. Rest of the time, the timer click event will assign the value.

0

The problem was that Visual Studio hosted it on localhost. If you use ip-adress 127.0.0.1 instead of local host within the URL it worked fast. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.