# `$timeout` (service in module `ng` )

Improve this doc

## Description

Angular's wrapper for `window.setTimeout`. The `fn` function is wrapped into a try/catch block and delegates any exceptions to `$exceptionHandler` service.

The return value of registering a timeout function is a promise which will be resolved when the timeout is reached and the timeout function is executed.

To cancel a the timeout request, call `$timeout.cancel(promise)`.

In tests you can use `$timeout.flush()` to synchronously flush the queue of deferred functions.

## Dependencies

- `$browser`

## Usage

    $timeout(fn[, delay][, invokeApply]);

### Parameters

- `fn – {function()} –`

  A function, who's execution should be delayed.

- `delay(optional=0) – {number=} –`

  Delay in milliseconds.

- `invokeApply(optional=true) – {boolean=} –`

  If set to false skips model dirty checking, otherwise will invoke `fn` within the `$apply` block.

### Returns

`{Promise}` –

Promise that will be resolved when the timeout is reached. The value this promise will be resolved with is the return value of the `fn` function.

## Methods

- ### cancel(promise)

  Cancels a task associated with the `promise`. As a result of this the promise will be resolved with a rejection.

  #### Parameters

  - `promise(optional) – {Promise=} –`

    Promise returned by the `$timeout` function.

  #### Returns

  `{boolean}` –

  Returns `true` if the task hasn't executed yet and was successfully canceled.