Stop/Restart $timeout when change and return to view


#1

Hello,

I have a View that is updated after 1 minute, I stop the timer after before leaving this view, and all is OK.
After returning to the current view the timer don’t restart again.

This is the code of the controller of this view:

.controller('IndexCtrl', function($scope, $timeout, RestService) {
    var updateN = 60*1000;
    $scope.test = "View 1 - Update";
    var update = function update() {
         timer = $timeout(update, updateN);
         /** make a http call to Rest API service and get data **/
        RestService.getdata(function(data) {;
             $scope.items = data.slice(0,2);
         });
    }();
    
   /** Stop the timer before leave the view**/
    $scope.$on('$ionicView.beforeLeave', function(){
   $timeout.cancel(timer);
      //alert("Before Leave");
   });  

   /** Restart timer **/
    $scope.$on('$ionicView.enter', function(){
    $timeout(update, updateN);
      //alert("Enter");
   }); 
})

.controller('ViewCtrl2', function($scope) {

  $scope.test = "View 2";

});

What I’m missing ?

Thanks