title: Using $window.onbeforeunload to save data then log out of firebase database
body: <p>When a user closes or refreshes a browser I would like to save the time to a firebase database and then log them out of firebase.</p>
  
  <p>I can do one or the other using <code>$window.onbeforeunload</code> but fail when I try to do both at the same time.</p>
  
  <p>I can save the time by doing the following:</p>
  
  <pre><code>    $scope.onExit = function () {         
        userRef.update({ lastLogin: Firebase.ServerValue.TIMESTAMP });    
      };
      $window.onbeforeunload = $scope.onExit;
  </code></pre>
  
  <p>or I can log the user out (using the logout service) by:</p>
  
  <pre><code>    $scope.onExit = function () {         
        logOutService.logoutUser();    
      };
      $window.onbeforeunload = $scope.onExit;
  </code></pre>
  
  <p>If I combine the two only the first item works (in the example below only the time gets logged)</p>
  
  <p>I have creating as separate functions and combining them but still only the first one works.</p>
  
  <pre><code>    $scope.onExit = function () {         
        userRef.update({ lastLogin: Firebase.ServerValue.TIMESTAMP });    
      };
  
      $scope.logOut = function () {         
        logOutService.logoutUser();    
      };
  
  
      $scope.combined = function () {
        $scope.logout();
        $scope.onExit();
      };
  
      $window.onbeforeunload = $scope.combined;
  </code></pre>
  
  <p>Any pointers greatly appreciated!</p>
  
  <p><strong>Update</strong></p>
  
  <p>Ok the above does work if I put the <code>$scope.onExit();</code> before the <code>$scope.logout();</code> (I simplified the scenario for the question). The issue happens when I put in a third function like below:</p>
  
  <pre><code>$scope.onExit1 = function () {
      userRef.update({ lastLoginPrevious: data.lastLogin });
  };
  
  $scope.onExit = function () {
      userRef.update({ lastLogin: Firebase.ServerValue.TIMESTAMP });
  };
  
  $scope.logOut = function () {         
     logOutService.logoutUser();    
  };
  
  
  $scope.combined = function () {
      $scope.onExit1();
      $scope.onExit();
      $scope.logout();
  };
  
  $window.onbeforeunload = $scope.combined;
  </code></pre>
  
  <p>where the new function is moving the current <code>lastLogin</code> time against a user to the <code>lastLoginPrevious</code>. I am doing this by using a <code>snapshot()</code> to get the <code>data.lastlogin</code> value and this seems to be what is causing the issue as if I change the <code>data.lastlogin</code> to <code>Firebase.ServerValue.TIMESTAMP</code> it works fine with three functions. Any ideas why I cannot use a <code>snapshot()</code> in this scenario? </p>
  
  <p><strong>Update</strong></p>
  
  <p>OK, I got to the bottom of this. I was trying to call <code>data.lastlogin</code> from outside of the <code>snapshot()</code> therefore it wasn't working. Once i nested my functions in the <code>snapshot()</code> it all worked ok.</p>