title: AngularJS exit recursive function call
body: <p>I use the following function the fetch users from a REST API, paginated by offset. <code>On Success-Callback</code>, the function gets called again recursively with a new offset to fetch the next junk of users.</p>
  
  <p><strong>Problem:</strong> If I switch or leave the view, the <code>FetchAttendee-Function</code> runs until all users are fetched. However to increase performance, I would like to stop fetching for users.     </p>
  
  <pre><code>fetchAttendees(event_id, offset);
  
  function fetchAttendees(event_id, offset) {
      AttendeeFactory(offset).show({id: event_id}, 
          function success(response) {
              [ DO SOMETHING WITH RESPONSE ]
              fetchAttendees(event_id, offset);
          }, 
          function (error) {
          });
  }
  </code></pre>
  
  <p>So, is it possible to stop calling the <em>fetchAttendee-Function</em> on the <em>view leave Event</em>?</p>
  
  <pre><code>$scope.$on("$ionicView.leave", function(scopes, states) {
      [ ...] 
  });
  </code></pre>
  
  <p><strong>AttendeeFactory</strong></p>
  
  <pre><code>.factory('AttendeeFactory', function ($resource) {
      return function (offset) {
          return $resource('http://10.0.0.6:8000/backend/attendees/:id/', {}, {
              show: { method: 'GET', headers: {'attendee-offset': offset}, isArray: true }
          });
      };
  })
  </code></pre>