* @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)
| 20528 | * |
| 20529 | */ |
| 20530 | function timeout(fn, delay, invokeApply) { |
| 20531 | if (!isFunction(fn)) { |
| 20532 | invokeApply = delay; |
| 20533 | delay = fn; |
| 20534 | fn = noop; |
| 20535 | } |
| 20536 | |
| 20537 | var args = sliceArgs(arguments, 3), |
| 20538 | skipApply = (isDefined(invokeApply) && !invokeApply), |
| 20539 | deferred = (skipApply ? $$q : $q).defer(), |
| 20540 | promise = deferred.promise, |
| 20541 | timeoutId; |
| 20542 | |
| 20543 | timeoutId = $browser.defer(function() { |
| 20544 | try { |
| 20545 | deferred.resolve(fn.apply(null, args)); |
| 20546 | } catch (e) { |
| 20547 | deferred.reject(e); |
| 20548 | $exceptionHandler(e); |
| 20549 | } finally { |
| 20550 | delete deferreds[promise.$$timeoutId]; |
| 20551 | } |
| 20552 | |
| 20553 | if (!skipApply) $rootScope.$apply(); |
| 20554 | }, delay); |
| 20555 | |
| 20556 | promise.$$timeoutId = timeoutId; |
| 20557 | deferreds[timeoutId] = deferred; |
| 20558 | |
| 20559 | return promise; |
| 20560 | } |
| 20561 | |
| 20562 | |
| 20563 | /** |
nothing calls this directly
no test coverage detected