* @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)
| 11265 | * </example> |
| 11266 | */ |
| 11267 | function interval(fn, delay, count, invokeApply) { |
| 11268 | var hasParams = arguments.length > 4, |
| 11269 | args = hasParams ? sliceArgs(arguments, 4) : [], |
| 11270 | setInterval = $window.setInterval, |
| 11271 | clearInterval = $window.clearInterval, |
| 11272 | iteration = 0, |
| 11273 | skipApply = (isDefined(invokeApply) && !invokeApply), |
| 11274 | deferred = (skipApply ? $$q : $q).defer(), |
| 11275 | promise = deferred.promise; |
| 11276 | |
| 11277 | count = isDefined(count) ? count : 0; |
| 11278 | |
| 11279 | promise.then(null, null, (!hasParams) ? fn : function() { |
| 11280 | fn.apply(null, args); |
| 11281 | }); |
| 11282 | |
| 11283 | promise.$$intervalId = setInterval(function tick() { |
| 11284 | deferred.notify(iteration++); |
| 11285 | |
| 11286 | if (count > 0 && iteration >= count) { |
| 11287 | deferred.resolve(iteration); |
| 11288 | clearInterval(promise.$$intervalId); |
| 11289 | delete intervals[promise.$$intervalId]; |
| 11290 | } |
| 11291 | |
| 11292 | if (!skipApply) $rootScope.$apply(); |
| 11293 | |
| 11294 | }, delay); |
| 11295 | |
| 11296 | intervals[promise.$$intervalId] = deferred; |
| 11297 | |
| 11298 | return promise; |
| 11299 | } |
| 11300 | |
| 11301 | |
| 11302 | /** |