title: Keep Session Alive MVC3
body: <p>I have looked at various jquery, ajax, image reload implementations and tried them all. None of them have worked for me. </p>
  
  <p>I am trying to implement the heartbeat() function in the following code snippet. This heartbeat needs to keep my session alive for as long as the user is either typing or clicking on the page. It should timeout after the set time and automatically redirect the user to the login page.  </p>
  
  <p>The problem I am running into is that even though it doesn't redirect the user to the login page, when the user clicks "submit" on the form it redirects them to the login page since the session has timed out. </p>
  
  <pre><code>document.onkeypress = function(e) {
      heartbeat();
      clearTimeout(timer);
      timer = setTimeout('RedirectToWelcomePage()',parseInt(sessionTimeout) * 30 * 1000);
  };
  
  document.onclick = function(e) {
      heartbeat();
      clearTimeout(timer);    
      timer = setTimeout('RedirectToWelcomePage()',parseInt(sessionTimeout) * 30 * 1000);
  };
  
  
  function heartbeat() {// need to implement
  }
  
  
  function RedirectToWelcomePage()
  {
      window.location.replace("/Account/LogOn");
  }
  </code></pre>