* @ngdoc service * @name $interval * * @description * Angular's wrapper for `window.setInterval`. The `fn` function is executed every `delay` * milliseconds. * * The return value of registering an interval function is a promise. This promise will be *
(fn, delay, count, invokeApply)
| 9125 | * </example> |
| 9126 | */ |
| 9127 | function interval(fn, delay, count, invokeApply) { |
| 9128 | var setInterval = $window.setInterval, |
| 9129 | clearInterval = $window.clearInterval, |
| 9130 | deferred = $q.defer(), |
| 9131 | promise = deferred.promise, |
| 9132 | iteration = 0, |
| 9133 | skipApply = (isDefined(invokeApply) && !invokeApply); |
| 9134 | |
| 9135 | count = isDefined(count) ? count : 0; |
| 9136 | |
| 9137 | promise.then(null, null, fn); |
| 9138 | |
| 9139 | promise.$$intervalId = setInterval(function tick() { |
| 9140 | deferred.notify(iteration++); |
| 9141 | |
| 9142 | if (count > 0 && iteration >= count) { |
| 9143 | deferred.resolve(iteration); |
| 9144 | clearInterval(promise.$$intervalId); |
| 9145 | delete intervals[promise.$$intervalId]; |
| 9146 | } |
| 9147 | |
| 9148 | if (!skipApply) $rootScope.$apply(); |
| 9149 | |
| 9150 | }, delay); |
| 9151 | |
| 9152 | intervals[promise.$$intervalId] = deferred; |
| 9153 | |
| 9154 | return promise; |
| 9155 | } |
| 9156 | |
| 9157 | |
| 9158 | /** |
nothing calls this directly
no test coverage detected