* @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)
| 13424 | * </example> |
| 13425 | */ |
| 13426 | function interval(fn, delay, count, invokeApply) { |
| 13427 | var hasParams = arguments.length > 4, |
| 13428 | args = hasParams ? sliceArgs(arguments, 4) : [], |
| 13429 | setInterval = $window.setInterval, |
| 13430 | clearInterval = $window.clearInterval, |
| 13431 | iteration = 0, |
| 13432 | skipApply = (isDefined(invokeApply) && !invokeApply), |
| 13433 | deferred = (skipApply ? $$q : $q).defer(), |
| 13434 | promise = deferred.promise; |
| 13435 | |
| 13436 | count = isDefined(count) ? count : 0; |
| 13437 | |
| 13438 | promise.$$intervalId = setInterval(function tick() { |
| 13439 | if (skipApply) { |
| 13440 | $browser.defer(callback); |
| 13441 | } else { |
| 13442 | $rootScope.$evalAsync(callback); |
| 13443 | } |
| 13444 | deferred.notify(iteration++); |
| 13445 | |
| 13446 | if (count > 0 && iteration >= count) { |
| 13447 | deferred.resolve(iteration); |
| 13448 | clearInterval(promise.$$intervalId); |
| 13449 | delete intervals[promise.$$intervalId]; |
| 13450 | } |
| 13451 | |
| 13452 | if (!skipApply) $rootScope.$apply(); |
| 13453 | |
| 13454 | }, delay); |
| 13455 | |
| 13456 | intervals[promise.$$intervalId] = deferred; |
| 13457 | |
| 13458 | return promise; |
| 13459 | |
| 13460 | function callback() { |
| 13461 | if (!hasParams) { |
| 13462 | fn(iteration); |
| 13463 | } else { |
| 13464 | fn.apply(null, args); |
| 13465 | } |
| 13466 | } |
| 13467 | } |
| 13468 | |
| 13469 | |
| 13470 | /** |