3.0.0 • Published 5 years ago

@amatiasq/scheduler v3.0.0

Weekly downloads
11
License
MIT
Repository
github
Last release
5 years ago

Scheduler

A smarter timer.

Installation

Install with npm i --save @amatiasq/scheduler.

Usage

import { Scheduler } from '@amatiasq/scheduler';

// for test purposes
function sleep(milliseconds: number) {
  return new Promise(resolve => setTimeout(resolve, milliseconds));
}

const halfSecond = 500;
const oneSecond = 1000;
const twoSeconds = 2000;
const scheduler = new Scheduler(oneSecond, () => console.log('TESTING'));

// nothing will happen until we call `start()` or `restart()`
await sleep(twoSeconds);

scheduler.start(); // start countdown

// If we keep restarting we keep delaying the execution
await sleep(halfSecond);
scheduler.restart(); // restart countdown from 0
await sleep(halfSecond);
scheduler.restart(); // restart countdown from 0

await sleep(oneSecond);
// prints "TESTING"

// we can restart as many times as we want
scheduler.start();
await sleep(halfSecond);

// if we stop it will never be executed until we call `start()` again
scheduler.stop();
await sleep(oneSecond);
// nothing prints
3.0.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.1.0

5 years ago