title: pause a javascript before page refresh
body: <p>Im just a newbie in Javascript but I have some serious problem which I searched for many hours on different sources, so I decided to post this question.</p>
  
  <p>How do you stall/pause a javascript before an asp.net page is refreshed or closed?
  Here is the code:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
      window.onbeforeunload = confirmExit;
  
      function confirmExit() {
  
          var connection = new Ext.data.Connection({
              defaultHeaders: {
                  'Content-Type': 'application/json'
              }
          });
          connection.request({
              url: 'services/vault/deletewfinstace/'
          });
          refresh();
          //alert("Helloo");
      }
  
      function refresh() {
          setTimeout(function () { window.location.reload() }, 6000);
      }
  
  
  
  &lt;/script&gt;
  </code></pre>
  
  <p>In this code I have called a javascript function on the event: window.onbeforeunload, and tried to put a setTimeout() after the function has done its work, but still page refreshes or closes without completing the contents of the function properly. The contents inside the function will execute successfully when I use alert() at the end of the function, but it is somewhat annoying to the user. Please help. Thank you.</p>