* @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)
| 18251 | * |
| 18252 | */ |
| 18253 | function timeout(fn, delay, invokeApply) { |
| 18254 | if (!isFunction(fn)) { |
| 18255 | invokeApply = delay; |
| 18256 | delay = fn; |
| 18257 | fn = noop; |
| 18258 | } |
| 18259 | |
| 18260 | var args = sliceArgs(arguments, 3), |
| 18261 | skipApply = (isDefined(invokeApply) && !invokeApply), |
| 18262 | deferred = (skipApply ? $$q : $q).defer(), |
| 18263 | promise = deferred.promise, |
| 18264 | timeoutId; |
| 18265 | |
| 18266 | timeoutId = $browser.defer(function() { |
| 18267 | try { |
| 18268 | deferred.resolve(fn.apply(null, args)); |
| 18269 | } catch (e) { |
| 18270 | deferred.reject(e); |
| 18271 | $exceptionHandler(e); |
| 18272 | } |
| 18273 | finally { |
| 18274 | delete deferreds[promise.$$timeoutId]; |
| 18275 | } |
| 18276 | |
| 18277 | if (!skipApply) $rootScope.$apply(); |
| 18278 | }, delay); |
| 18279 | |
| 18280 | promise.$$timeoutId = timeoutId; |
| 18281 | deferreds[timeoutId] = deferred; |
| 18282 | |
| 18283 | return promise; |
| 18284 | } |
| 18285 | |
| 18286 | |
| 18287 | /** |
nothing calls this directly
no test coverage detected