-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoRefresh.js
42 lines (40 loc) · 1.51 KB
/
autoRefresh.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Created by Saurajit on 7/10/13.
*/
'use strict';
angular.module('autoRefresh', [])
.directive('autoRefresh', ['$timeout', function ($timeout) {//$timeout service injected
return {
restrict: 'A', //Only act as an HTML attribute
link: function (scope, element, attrs) {
/*
scope: scope of the controller where the directive has been added
element: element where the directive has been added
attrs: HTML attributes where the directive has been added
*/
if (isNaN(parseInt(scope.$eval(attrs.refreshInterval)), 10)) {
attrs.refreshInterval = 1000; //Defaults to 1 second
}
if (angular.isUndefined(attrs.refreshStop)) {
attrs.refreshStop = true; //Flag to set to stop auto refreshing. Defaults to true.
}
var repeatFunction = function () {
//'refreshInBackground' flag to check if to auto refresh even on route change.
if (angular.isUndefined(attrs.refreshInBackground) || !scope.$eval(attrs.refreshInBackground)) {
scope.$on('$routeChangeStart', function () {
attrs.refreshStop = false;
});
}
var promise = $timeout(function () {
if (!scope.$eval(attrs.refreshStop)) {
$timeout.cancel(promise);
return;
}
scope.$eval(attrs.autoRefresh);
repeatFunction();
}, scope.$eval(attrs.refreshInterval));
};
repeatFunction();
}
};
}]);