* @ngdoc service * @name $interval * * @description * AngularJS'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 * notifi
(fn, delay, count, invokeApply)
| 13591 | * </example> |
| 13592 | */ |
| 13593 | function interval(fn, delay, count, invokeApply) { |
| 13594 | var hasParams = arguments.length > 4, |
| 13595 | args = hasParams ? sliceArgs(arguments, 4) : [], |
| 13596 | setInterval = $window.setInterval, |
| 13597 | clearInterval = $window.clearInterval, |
| 13598 | iteration = 0, |
| 13599 | skipApply = (isDefined(invokeApply) && !invokeApply), |
| 13600 | deferred = (skipApply ? $$q : $q).defer(), |
| 13601 | promise = deferred.promise; |
| 13602 | |
| 13603 | count = isDefined(count) ? count : 0; |
| 13604 | |
| 13605 | promise.$$intervalId = setInterval(function tick() { |
| 13606 | if (skipApply) { |
| 13607 | $browser.defer(callback); |
| 13608 | } else { |
| 13609 | $rootScope.$evalAsync(callback); |
| 13610 | } |
| 13611 | deferred.notify(iteration++); |
| 13612 | |
| 13613 | if (count > 0 && iteration >= count) { |
| 13614 | deferred.resolve(iteration); |
| 13615 | clearInterval(promise.$$intervalId); |
| 13616 | delete intervals[promise.$$intervalId]; |
| 13617 | } |
| 13618 | |
| 13619 | if (!skipApply) $rootScope.$apply(); |
| 13620 | |
| 13621 | }, delay); |
| 13622 | |
| 13623 | intervals[promise.$$intervalId] = deferred; |
| 13624 | |
| 13625 | return promise; |
| 13626 | |
| 13627 | function callback() { |
| 13628 | if (!hasParams) { |
| 13629 | fn(iteration); |
| 13630 | } else { |
| 13631 | fn.apply(null, args); |
| 13632 | } |
| 13633 | } |
| 13634 | } |
| 13635 | |
| 13636 | |
| 13637 | /** |