* @ngdoc service * @name $timeout * * @description * Angular'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 retu
(fn, delay, invokeApply)
| 18727 | * |
| 18728 | */ |
| 18729 | function timeout(fn, delay, invokeApply) { |
| 18730 | if (!isFunction(fn)) { |
| 18731 | invokeApply = delay; |
| 18732 | delay = fn; |
| 18733 | fn = noop; |
| 18734 | } |
| 18735 | |
| 18736 | var args = sliceArgs(arguments, 3), |
| 18737 | skipApply = (isDefined(invokeApply) && !invokeApply), |
| 18738 | deferred = (skipApply ? $$q : $q).defer(), |
| 18739 | promise = deferred.promise, |
| 18740 | timeoutId; |
| 18741 | |
| 18742 | timeoutId = $browser.defer(function() { |
| 18743 | try { |
| 18744 | deferred.resolve(fn.apply(null, args)); |
| 18745 | } catch (e) { |
| 18746 | deferred.reject(e); |
| 18747 | $exceptionHandler(e); |
| 18748 | } |
| 18749 | finally { |
| 18750 | delete deferreds[promise.$$timeoutId]; |
| 18751 | } |
| 18752 | |
| 18753 | if (!skipApply) $rootScope.$apply(); |
| 18754 | }, delay); |
| 18755 | |
| 18756 | promise.$$timeoutId = timeoutId; |
| 18757 | deferreds[timeoutId] = deferred; |
| 18758 | |
| 18759 | return promise; |
| 18760 | } |
| 18761 | |
| 18762 | |
| 18763 | /** |
nothing calls this directly
no test coverage detected