From 579241db921a595ac46df4de43a1a03ed759f7ae Mon Sep 17 00:00:00 2001
From: lpfeup <lpfeup@users.noreply.github.com>
Date: Sat, 21 Jan 2017 05:04:28 +0000
Subject: [PATCH] #503 fix(container-stats): fix container stats timer not
 being properly canceled. (#504)

---
 app/components/stats/statsController.js | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/app/components/stats/statsController.js b/app/components/stats/statsController.js
index eed96ef18..58064f980 100644
--- a/app/components/stats/statsController.js
+++ b/app/components/stats/statsController.js
@@ -114,6 +114,12 @@ function (Settings, $scope, Messages, $timeout, Container, ContainerTop, $stateP
     });
     $scope.networkLegend = $sce.trustAsHtml(networkChart.generateLegend());
 
+    function setUpdateStatsTimeout() {
+      if(!destroyed) {
+        timeout = $timeout(updateStats, 5000);
+      }
+    }
+
     function updateStats() {
       Container.stats({id: $stateParams.id}, function (d) {
         var arr = Object.keys(d).map(function (key) {
@@ -129,15 +135,17 @@ function (Settings, $scope, Messages, $timeout, Container, ContainerTop, $stateP
         updateCpuChart(d);
         updateMemoryChart(d);
         updateNetworkChart(d);
-        timeout = $timeout(updateStats, 5000);
+        setUpdateStatsTimeout();
       }, function () {
         Messages.error('Unable to retrieve stats', {}, 'Is this container running?');
-        timeout = $timeout(updateStats, 5000);
+        setUpdateStatsTimeout();
       });
     }
 
+    var destroyed = false;
     var timeout;
     $scope.$on('$destroy', function () {
+      destroyed = true;
       $timeout.cancel(timeout);
     });