Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJs & Ionic : Stop/Restart $timeout when change and return to view

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";

});
like image 466
ingalb Avatar asked Sep 24 '15 10:09

ingalb


People also ask

What AngularJS used for?

AngularJS is a structural framework for dynamic web apps. It lets you use HTML as your template language and lets you extend HTML's syntax to express your application's components clearly and succinctly. AngularJS's data binding and dependency injection eliminate much of the code you would otherwise have to write.

Is AngularJS better than Nodejs?

Node. js is more preferable when faster and scalable web development is needed. It is usually used for building small-sized projects. Angular is preferred when real-time applications, for example, chat apps, or instant messaging are needed.

What is Angular vs AngularJS?

Angular uses TypeScript and has components as its main building blocks. It is component-based, whereas AngularJS uses directives. Angular's operation employs a hierarchy of components, while AngularJS has directives that allow code reusability. So, The AngularJS framework provides reusable components for its users.

Which is better PHP or AngularJS?

As Angular uses TypeScript, it executes faster than PHP when measured on a single hardware. However, as it primarily works on the client-side, if a user's machine isn't powerful enough, so will Angular's performance.


1 Answers

I resolve the problem, There is not a problem with the cache, but with the function update that is not called after I re-enter on the page. I move the update function inside the $ionicView.enter : The corrected code is:

   $scope.$on('$ionicView.beforeLeave', function(){
      //updateN=12000000;
      $timeout.cancel(timer);
      //alert("Leave");
   });

  $scope.$on('$ionicView.enter', function(){
      //updateN=12000000;
    var update = function update() {
    timer = $timeout(update, updateN);
    RestService.getdata(function(data) {
        //console.log(tani);
        //$scope.items = data;
        $scope.items = data.slice(0,2);
    });
   }();
   });
like image 72
ingalb Avatar answered Sep 27 '22 22:09

ingalb