* @ngdoc service * @name $timeout * * @description * AngularJS's wrapper for `window.setTimeout`. The `fn` function is wrapped into a try/catch * block and delegates any exceptions to * ng.$exceptionHandler $exceptionHandler service. * * The return val
(fn, delay, invokeApply)
| 21579 | * |
| 21580 | */ |
| 21581 | function timeout(fn, delay, invokeApply) { |
| 21582 | if (!isFunction(fn)) { |
| 21583 | invokeApply = delay; |
| 21584 | delay = fn; |
| 21585 | fn = noop; |
| 21586 | } |
| 21587 | |
| 21588 | var args = sliceArgs(arguments, 3), |
| 21589 | skipApply = (isDefined(invokeApply) && !invokeApply), |
| 21590 | deferred = (skipApply ? $$q : $q).defer(), |
| 21591 | promise = deferred.promise, |
| 21592 | timeoutId; |
| 21593 | |
| 21594 | timeoutId = $browser.defer(function() { |
| 21595 | try { |
| 21596 | deferred.resolve(fn.apply(null, args)); |
| 21597 | } catch (e) { |
| 21598 | deferred.reject(e); |
| 21599 | $exceptionHandler(e); |
| 21600 | } finally { |
| 21601 | delete deferreds[promise.$$timeoutId]; |
| 21602 | } |
| 21603 | |
| 21604 | if (!skipApply) $rootScope.$apply(); |
| 21605 | }, delay, '$timeout'); |
| 21606 | |
| 21607 | promise.$$timeoutId = timeoutId; |
| 21608 | deferreds[timeoutId] = deferred; |
| 21609 | |
| 21610 | return promise; |
| 21611 | } |
| 21612 | |
| 21613 | |
| 21614 | /** |
nothing calls this directly
no test coverage detected