# timer-queue

> time-delayed function queue

Latest version **1.1.0** (published 2020-03-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install timer-queue
pnpm add timer-queue
yarn add timer-queue
bun add timer-queue
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2020-03-28 |
| First published | 2018-02-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 73.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Hiroaki Sano |
| Maintainers | hirosan18 |
| Keywords | async, asynchronous, job, queue, queues, task, timer |

## Links

- npm: https://www.npmjs.com/package/timer-queue
- Repository: https://github.com/hirosan18/timer-queue
- Homepage: https://github.com/hirosan18/timer-queue#readme
- Issues: https://github.com/hirosan18/timer-queue/issues
- npm.io page: https://npm.io/package/timer-queue

## Dependencies (1)

- [sleep-promise](https://npm.io/package/sleep-promise.md) ^8.0.1

## 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.1.0 (latest) — 2020-03-28
- 1.0.0 — 2018-04-27
- 0.1.0 — 2018-03-01
- 0.0.3 — 2018-02-19
- 0.0.2-rc.1 — 2018-02-18
- 0.0.1 — 2018-02-15

## README

# timer-queue

[![npm version](https://badge.fury.io/js/timer-queue.svg)](https://badge.fury.io/js/timer-queue)
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)

TimerQueue is for controlling the execution timing of function

## Installation

```
npm install timer-queue
```

## Usage - API

### class TimerQueue

#### var tqueue = new TimerQueue([options])

Constructor

- options `Object` - may contain inital values
- options.interval `number` - interval time (ms) for execution. `default: 0`
- options.timeout `number` - timeout time (ms) for execution. `default: 0`
- options.retry `number` - retry count when execution function failed. `default: 0`
- options.retryInterval `number` - interval time (ms) for retry execution. `default: 0`
- options.autostart `boolean` - auto start when enqueue by `tqueue.push()`. `default: false`
- options.startImmediately `boolean` - run immediately when queue is empty and enqueuing by `tqueue.push()`. `default: false`

#### tqueue.start()

start the queue.

#### tqueue.stop()

stop the queue.

can be resumed with `tqueue.start()` and can be empty with `tqueue.clear()`.

#### tqueue.clear()

empty the queue.

#### tqueue.push(fn[, delay])

enqueue the function.

- fn `function` - execution function
- delay `number` - delay time (ms) for execution. `defalut: 0`

#### tqueue.push(fn[, options])

enqueue the function.

- fn `function` - execution function
- options `Object` - may contain options values
- options.delay `number` - delay time (ms) for execution. `defalut: 0`
- options.retry `number` - retry count when execution function failed. `default: 0`
- options.retryInterval `number` - interval time (ms) for retry execution. `default: 0`
- options.error `function` - error callback.

## Example

```js
import TimerQueue from 'timer-queue'

let count = 0
const now = Date.now()

const tqueue = new TimerQueue({
  interval: 1000,
  timeout: 10000,
  retry: 3,
  retryInterval: 200,
  autostart: false
})

tqueue.push(() => {
  const result = count++ > 1
  console.log(`first: ${Date.now() - now}ms / return ${result}`)
  return result // return Boolean or not return
})

tqueue.push(() => {
  count = 0 // reset counter
})

tqueue.push((done, error) => {
  const result = count++ > 1
  setTimeout(() => {
    console.log(`second: ${Date.now() - now}ms / ${result ? 'done()' : 'error()'}`)
    result ? done() : error() // execute a arguments callback
  }, 0)
})

tqueue.push(() => {
  count = 0 // reset counter
})

tqueue.push(() => {
  const result = count++ > 1
  const promise = new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log(`third: ${Date.now() - now}ms / ${result ? 'resolve()' : 'reject()'}`)
      result ? resolve() : reject(new Error('retry'))
    }, 0)
  })
  return promise // return a promise
})

tqueue.on('end', () => {
  console.log(`end: ${Date.now() - now}ms`)
})
tqueue.on('error', () => {
  console.log(`error: ${Date.now() - now}ms`)
})

tqueue.start()

// => first: 3ms / return false
// => first: 212ms / return false
// => first: 414ms / return true
// => second: 2421ms / error()
// => second: 2625ms / error()
// => second: 2832ms / done()
// => third: 4841ms / reject()
// => third: 5046ms / reject()
// => third: 5250ms / resolve()
// => end: 5251ms
```

```js
import TimerQueue from 'timer-queue'

const now = Date.now()

const tqueue = new TimerQueue({
  interval: 1000,
  timeout: 10000,
  retry: 3,
  retryInterval: 200,
  autostart: false
})

tqueue.push(() => {
  console.log(`first: ${Date.now() - now}ms / return false`)
  return false
})

tqueue.push(() => {
  console.log(`second: not execute`)
})

tqueue.on('end', () => {
  console.log(`end: ${Date.now() - now}ms`)
})
tqueue.on('error', () => {
  console.log(`error: ${Date.now() - now}ms`)
})

tqueue.start()


// => first: 4ms / return false
// => first: 211ms / return false
// => first: 419ms / return false
// => first: 623ms / return false
// => error: 624ms
```

## Development

```
# build
npm run build
# test
npm run test
```

## License

MIT

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