@this
()
| 21543 | |
| 21544 | /** @this */ |
| 21545 | function $TimeoutProvider() { |
| 21546 | this.$get = ['$rootScope', '$browser', '$q', '$$q', '$exceptionHandler', |
| 21547 | function($rootScope, $browser, $q, $$q, $exceptionHandler) { |
| 21548 | |
| 21549 | var deferreds = {}; |
| 21550 | |
| 21551 | |
| 21552 | /** |
| 21553 | * @ngdoc service |
| 21554 | * @name $timeout |
| 21555 | * |
| 21556 | * @description |
| 21557 | * AngularJS's wrapper for `window.setTimeout`. The `fn` function is wrapped into a try/catch |
| 21558 | * block and delegates any exceptions to |
| 21559 | * {@link ng.$exceptionHandler $exceptionHandler} service. |
| 21560 | * |
| 21561 | * The return value of calling `$timeout` is a promise, which will be resolved when |
| 21562 | * the delay has passed and the timeout function, if provided, is executed. |
| 21563 | * |
| 21564 | * To cancel a timeout request, call `$timeout.cancel(promise)`. |
| 21565 | * |
| 21566 | * In tests you can use {@link ngMock.$timeout `$timeout.flush()`} to |
| 21567 | * synchronously flush the queue of deferred functions. |
| 21568 | * |
| 21569 | * If you only want a promise that will be resolved after some specified delay |
| 21570 | * then you can call `$timeout` without the `fn` function. |
| 21571 | * |
| 21572 | * @param {function()=} fn A function, whose execution should be delayed. |
| 21573 | * @param {number=} [delay=0] Delay in milliseconds. |
| 21574 | * @param {boolean=} [invokeApply=true] If set to `false` skips model dirty checking, otherwise |
| 21575 | * will invoke `fn` within the {@link ng.$rootScope.Scope#$apply $apply} block. |
| 21576 | * @param {...*=} Pass additional parameters to the executed function. |
| 21577 | * @returns {Promise} Promise that will be resolved when the timeout is reached. The promise |
| 21578 | * will be resolved with the return value of the `fn` function. |
| 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 | } |
nothing calls this directly
no test coverage detected