Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent asp:Timer from sending tick before response is generated?

I have an ASP.NET web site. I want a user to make a decision and click one of the given buttons within a certain amount of time and redirect him to another page. If he didn't make a decision, he still will be redirected with "no choice made" mark in my head.

I use asp:Timer to initiate postback after time is up. I'm doing redirect by calling Response.Redirect('page.html', false) in Page_Load handler. In this case I can handle button_click or timer_tick events before actually redirecting. After one of those events is processed, page is rendered and redirect is done.

But I have two problems. First, if input is processed long enough, second timer's tick is triggered, new postback initiated, and my current is canceled! Second, if user click a button just before the time is up, the timer tick occurs before redirect is done and it again overrides handling of user click!

How can I resolve these issues? How can I disable timer on client side after the first tick is triggered or after a user have clicked a button? Or what is the better way of solving my task? Thanks.

like image 744
Mikhail Avatar asked Nov 03 '22 15:11

Mikhail


2 Answers

As a variant you may use client-side setTimeout function instead of Timer control and clear timeout on button click:

 <script type="text/javascript">
      var redirectTimeout = setTimeout(function () {
           location.href = '<%= ResolveClientUrl("~/page.html") %>';
      }, 5000);
 </script>

 <asp:Button runat="server" ID="SubmitButton" Text="Submit" 
      OnClientClick="clearTimeout(redirectTimeout);" />
like image 139
Yuriy Rozhovetskiy Avatar answered Nov 08 '22 05:11

Yuriy Rozhovetskiy


I got the solution for both problems.

  1. I placed asp:Timer inside an asp:UpdatePanel control. This is the right thing to do in my case since I don't need to refresh entire page before the next question. But I discovered that in this case ticks from timer don't interfere with each other.

  2. As described here, I can disable asp:Timer using javascript like this:

    $find("TimerMaxJudgeTime")._stopTimer();
    

    Then I bind this to my buttons' onclick event and I'm done. Since timer is placed inside an UpdatePanel, it is autmatically recreated and enabled after a postback.

like image 39
Mikhail Avatar answered Nov 08 '22 04:11

Mikhail