# chain-timeout

> Execute Chained Timeout functions using requestAnimationFrame

Latest version **1.0.6** (published 2020-09-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install chain-timeout
pnpm add chain-timeout
yarn add chain-timeout
bun add chain-timeout
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: esm support; no vulnerabilities; high maintenance score.

Warnings: low downloads; no types.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2020-09-28 |
| First published | 2019-04-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Jorge Moreno @alterebro |
| Maintainers | alterebro |
| Keywords | chain, setTimeout, timeout, requestAnimationFrame, chainable, delay, queue |

## Links

- npm: https://www.npmjs.com/package/chain-timeout
- Repository: https://github.com/alterebro/chain-timeout
- Homepage: https://github.com/alterebro/chain-timeout#readme
- Issues: https://github.com/alterebro/chain-timeout/issues
- npm.io page: https://npm.io/package/chain-timeout

## Alternatives

- [cron](https://npm.io/package/cron.md) — 4.9M weekly downloads
- [@vercel/queue](https://npm.io/package/@vercel/queue.md) — 731.6K weekly downloads
- [create-sonicjs](https://npm.io/package/create-sonicjs.md) — 1.6K weekly downloads
- [@exellix/jobs-api](https://npm.io/package/@exellix/jobs-api.md) — 941 weekly downloads
- [@forwardimpact/libskill](https://npm.io/package/@forwardimpact/libskill.md) — 575 weekly downloads

## Recent versions

- 1.0.6 (latest) — 2020-09-28
- 1.0.5 — 2020-07-18
- 1.0.4 — 2020-05-09
- 1.0.3 — 2020-03-14
- 1.0.2 — 2019-11-14
- 1.0.1 — 2019-11-14
- 1.0.0 — 2019-04-24
- 0.1.0 — 2019-04-24

## README

<p>
    <img src="static/chain-timeout.png" alt="chain-timeout" width="175" />
</p>

[![MIT license](https://img.shields.io/github/license/alterebro/chain-timeout.svg)](https://github.com/alterebro/chain-timeout/blob/master/LICENSE) [![NPM Version](https://img.shields.io/npm/v/chain-timeout.svg)](https://www.npmjs.com/package/chain-timeout) [![gzip File Size](http://img.badgesize.io/https://unpkg.com/chain-timeout/dist/chain-timeout.min.js?compression=gzip)](https://unpkg.com/chain-timeout/dist/chain-timeout.min.js) [![Twitter Follow](https://img.shields.io/twitter/follow/alterebro.svg?color=%2338A1F3&style=popout)](https://twitter.com/alterebro)


# chain-timeout

**Execute chained setTimeout functions using requestAnimationFrame to improve performance**. <br>By using `requestAnimationFrame` the timeout defined will only run when the document is active, if the document browser or tab is placed on background the script won't be executed therefore performance will not be affected and battery life will be saved :)


## Install

```sh
$ npm install chain-timeout
```

This module is available in three flavours:

| Flavour       | Path          |
| ------------- | ------------- |
| **ES Module** | `dist/chain-timeout.mjs`      |
| **CommonJS**  | `dist/chain-timeout.js`       |
| **UMD**       | `dist/chain-timeout.min.js`   |

Include `chain-timeout`:

```javascript
// ES6
import chainTimeout from 'chain-timeout'

// CJS
const chainTimeout = require('chain-timeout');
```

To use it on the browser include the UMD minified file ( `dist/chain-timeout.min.js` ).
You can also add the script directly from [unpkg.com](https://unpkg.com/chain-timeout):

```html
<script src="https://unpkg.com/chain-timeout"></script>
```


## Usage

Include it and create a *chain* :

```javascript
import chainTimeout from 'chain-timeout';

const chain = chainTimeout( fn, delay ).chainTimeout( fn, delay )...
```

Example :

```javascript
const chain = chainTimeout( () => console.log('hello!'), 2000 )
    .chainTimeout( () => console.log('...'), 3000 )
    .chainTimeout( () => console.log('bye!'), 1000 );
/*
    -> Outputs the string 'hello!' after 2000ms.
    -> Then outputs the string '...' 3000ms after the first message.
    -> Finally outputs 'bye!' when 1000ms have passed since previous function.
*/
```


## API

### chainTimeout(function, delay)

Returns an `Object` with `chainTimeout()` itself to get chained and `clear()` method to cancel the execution chain.

#### &mdash; function

Type : `function`. Function to get executed once the time set on the delay is reached

#### &mdash; delay

Type : `number`. Number defining the milliseconds to wait before the function is executed.

### chain.clear()

Cancels the execution chain.

```javascript
const chain = chainTimeout(fn, delay)
	.chainTimeout(fn, delay)
	.chainTimeout(fn, delay):

chain.clear() // Cancels chain
```

## License

MIT © 2019 [Jorge Moreno](https://moro.es), [@alterebro](https://twitter.com/alterebro)

---
_Source: https://npm.io/package/chain-timeout · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
