Arter
Arter

Reputation: 187

angular timeout reload after page change

I use angular $time out to reload data from api and show live statistic in dom. Reload works very well, make reload after 5 sec. But problem is, after i click on another tab, where i don't need $timeout and reload data, there is still reloading data same like on page before. That data is not in DOM, but in network console still reloading data from code bellow, and call http get url with that data.

$scope.reload = function () {
$http.get(serviceBase + 'live-stats').
    success(function (res) {
         $scope.proizvedeno = res;
console.log(res);  
         });
         $timeout(function(){
            $scope.reload();
            }, 5000);
    };
            $scope.reload(); 

P.S. i use angular-loading-bar, is there any way to this bar does not rotate when reload only a certain part of the page, in this case retrieve data from the "timeout" to display statistics

EDIT: i add this to this ctrl

$scope.reload = function () {
$http.get(serviceBase + 'live-stats').
    success(function (res) {
         $scope.proizvedeno = res;
console.log(res);  
         });
         $timeout(function(){
            $scope.reload();
            }, 5000);
 $scope.$on('$destroy', function(){    //this is what i add
$timeout.cancel(reload);             //this is what i add
    };
            $scope.reload(); 

but still timeout not canceled

Upvotes: 2

Views: 1100

Answers (1)

Arter
Arter

Reputation: 187

i only need to declare "var reload"

var reload = $timeout(function(){
            $scope.reload();
            }, 5000);
            $scope.$on('$destroy', function(){
$timeout.cancel(reload);

});

Upvotes: 1

Related Questions