title: Check if webpage is closed and run process
body: <p>I have a function that needs to know if the user has closed a page (either by tab, or backing out, or whatever) and run a MVC Controller so I can run a few internal processes. Is this feasible with Javascript or AJAX or impossible? I have the following code:</p>
  
  <pre><code>$(window).bind('beforeunload', function (e) {
      // code to execute when browser is closed
      e.$.post("/Functions/Left" });
  
  });
  </code></pre>
  
  <p>But its not doing anything as far as I can see. The Controller processes some database information, and yes it has HTTPPOST.</p>
  
  <p>EDIT: Tried out the following:</p>
  
  <p>var unloaded = false;</p>
  
  <pre><code>                        $(window).on('beforeunload', unload);
                          $(window).on('unload', unload);
  
                          function unload() {
  
                              if (!unloaded) {
  
  
                                  $.ajax({
                                      type: 'get',
                                      async: false,
                                      url: '/Function/Left/@ViewBag.ID',
                                      success: function () {
                                          unloaded = true;
                                          $('body').css('cursor', 'default');
                                      },
                                      timeout: 5000
                                  });
                              }
                          }
  </code></pre>
  
  <p>Its not firing anything though, and I changed the controller to GET. </p>