title: How to run JavaScript prior to System.Web.UI.Timer OnTick?
body: <p>I need a client-side script to confirm that the Timer's OnTick event can proceed.  </p>
  
  <p>How do I invoke <em>CanContinue()</em> before the Timer1 postback occurs? <br /> 
  How do I cancel the postback if user selects Cancel?</p>
  
  <pre><code>&lt;%@ Page Language="C#" %&gt;
  &lt;script runat="server"&gt;
      protected void Timer1_OnTick(object sender, EventArgs e)
      {
          Response.Write("Last Postback: " + DateTime.Now.ToString());
      }
  &lt;/script&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;Timer Test&lt;/title&gt;
      &lt;script type="text/javascript" language="javascript"&gt;
          function CanContinue() {
              return confirm('The page is requesting new data.  Ok or Cancel?');
          }
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
          &lt;asp:ScriptManager ID="ScriptManager1" runat="server" /&gt;
          &lt;asp:Timer ID="Timer1" runat="server" OnTick="Timer1_OnTick" Interval="300000" /&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <hr>
  
  <p>NOTE: Some readers might take issue with showing a modal confirmation every x minutes.  I would agree that this is poor for usability, but I'm not in a position to change the design.</p>
  
  <hr>
  
  <p>UPDATE: I decided not to use a Timer after all.  See <a href="http://robertclaypool.info/2009/01/21/javascript-and-timer-ontick/" rel="nofollow noreferrer">this post</a> for details on an alternative solution.</p>