* @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)
| 12015 | * </example> |
| 12016 | */ |
| 12017 | function interval(fn, delay, count, invokeApply) { |
| 12018 | var hasParams = arguments.length > 4, |
| 12019 | args = hasParams ? sliceArgs(arguments, 4) : [], |
| 12020 | setInterval = $window.setInterval, |
| 12021 | clearInterval = $window.clearInterval, |
| 12022 | iteration = 0, |
| 12023 | skipApply = (isDefined(invokeApply) && !invokeApply), |
| 12024 | deferred = (skipApply ? $$q : $q).defer(), |
| 12025 | promise = deferred.promise; |
| 12026 | |
| 12027 | count = isDefined(count) ? count : 0; |
| 12028 | |
| 12029 | promise.$$intervalId = setInterval(function tick() { |
| 12030 | if (skipApply) { |
| 12031 | $browser.defer(callback); |
| 12032 | } else { |
| 12033 | $rootScope.$evalAsync(callback); |
| 12034 | } |
| 12035 | deferred.notify(iteration++); |
| 12036 | |
| 12037 | if (count > 0 && iteration >= count) { |
| 12038 | deferred.resolve(iteration); |
| 12039 | clearInterval(promise.$$intervalId); |
| 12040 | delete intervals[promise.$$intervalId]; |
| 12041 | } |
| 12042 | |
| 12043 | if (!skipApply) $rootScope.$apply(); |
| 12044 | |
| 12045 | }, delay); |
| 12046 | |
| 12047 | intervals[promise.$$intervalId] = deferred; |
| 12048 | |
| 12049 | return promise; |
| 12050 | |
| 12051 | function callback() { |
| 12052 | if (!hasParams) { |
| 12053 | fn(iteration); |
| 12054 | } else { |
| 12055 | fn.apply(null, args); |
| 12056 | } |
| 12057 | } |
| 12058 | } |
| 12059 | |
| 12060 | |
| 12061 | /** |