# tickable-timer

> Manual ticking timer API

Latest version **1.0.0** (published 2015-12-06) · MIT license · 0 weekly downloads

## Install

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

## 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.0.0 |
| Published | 2015-12-06 |
| First published | 2015-01-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Nao Yonamine |
| Maintainers | mohayonao |
| Keywords | clearInterval, clearTimeout, setInterval, setTimeout, test, timer |

## Links

- npm: https://www.npmjs.com/package/tickable-timer
- Repository: https://github.com/mohayonao/tickable-timer
- Homepage: https://github.com/mohayonao/tickable-timer/
- Issues: https://github.com/mohayonao/tickable-timer/issues
- npm.io page: https://npm.io/package/tickable-timer

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2015-12-06
- 0.3.0 — 2015-08-18
- 0.2.1 — 2015-05-21
- 0.2.0 — 2015-04-28
- 0.1.0 — 2015-01-23
- 0.0.1 — 2015-01-20

## README

# tickable-timer
[![Build Status](http://img.shields.io/travis/mohayonao/tickable-timer.svg?style=flat-square)](https://travis-ci.org/mohayonao/tickable-timer)
[![NPM Version](http://img.shields.io/npm/v/tickable-timer.svg?style=flat-square)](https://www.npmjs.org/package/tickable-timer)
[![License](http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](http://mohayonao.mit-license.org/)

> Manual ticking timer API for test CI

## Features
- Compatible with the native timer API
- Provides hook events before/after ticking

## Installation

```
npm install tickable-timer
```

## API

### Properties
- `timers: number[]`
- `intervals: number[]`
- `timeouts: number[]`

### Methods
_Inherits methods from [EventEmitter](https://nodejs.org/api/events.html)._
- `setTimeout(callback: function, delay: number, ...params: any): number`
- `clearTimeout(timerId: number): void`
- `setInterval(callback: function, delay: number, ...params: any): number`
- `clearInterval(timerId: number): void`
- `tick(tick: number = 1): void`
- `clearAllTimers(): void`

### Events
- `tick`
  - `tick: number` The ticking interval
- `ticked`
  - `tick: number` The ticking interval

## Usage

```javascript
function counter(start, stop, interval, timerAPI = global) {
  let emitter = new EventEmitter();
  let count = start;
  let timerId = timerAPI.setInterval(() => {
    emitter.emit("bang", count++);
    if (stop <= count) {
      emitter.emit("end");
      timerAPI.clearInterval(timerId);
    }
  }, interval);

  return emitter;
}

// in the production (use native timer)
counter(0, 10, 1000).on("bang", (count) => {
  console.log(`bang! ${count}`);
});

// in the test (use tickable timer)
import tickable from "tickable-timer";

counter(0, 10, 1000, tickable).on("bang", spy);
```

## Example

```javascript
import tickable from "tickable-timer";

tickable.setTimeout(() => {
  console.log("fired: timer1");
}, 1000);

tickable.setInterval(() => {
  console.log("fired: timer2");
}, 500);

tickable.setTimeout(() => {
  // never called
  console.log("fired: timer3");
}, 3000);

tickable.setInterval(() => {
  // never called
  console.log("fired: timer4");
}, 3000);

tickable.setTimeout(() => {
  console.log("clear: all timers");
  tickable.clearAllTimers();
  console.log("timers:", tickable.timers);
}, 2500);

tickable.on("tick", (tick) => {
  console.log(`tick: ${tick}`);
});
tickable.on("ticked", (tick) => {
  console.log(`ticked: ${tick}\n`);
});

tickable.tick(5000);
```

output:

```
tick: 500
fired: timer2
ticked: 500

tick: 500
fired: timer1
fired: timer2
ticked: 500

tick: 500
fired: timer2
ticked: 500

tick: 500
fired: timer2
ticked: 500

tick: 500
fired: timer2
clear: all timers
timers: []
ticked: 500

tick: 2500
ticked: 2500
```

## License

MIT

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