action-timer
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

ActionTimer

A class to execute an action every specified msec. The functions are only start, stop and notify. notify function interrupts the timer and execute the specified action if the action isn't executing. It does nothing while the action is executing. Use QueuedActionTimer if you want to execute the action after current action process is completed.

Usage

const instance = new ActionTimer(1000);
const action = () => {
    // your function
}
instance.action = action;
instance.start();
// action is called when the callback returns true
// the timer starts from 0 again
instance.notify(() => true);
instance.stop();

// action isn't called while timer stops
instance.notify(() => true);

Example output

See example code. It executes every second but it calls notify function after 2800 msec.

2021-07-12T04:29:51.358Z ---- timer start ----
2021-07-12T04:29:52.380Z call count: 1
2021-07-12T04:29:53.382Z call count: 2 // diff - 1000 msec
interrupt
2021-07-12T04:29:54.168Z call count: 3 // diff - 800 msec
2021-07-12T04:29:55.179Z call count: 4 // diff - 1000 msec
2021-07-12T04:29:56.249Z call count: 5 // diff - 1000 msec
2021-07-12T04:29:56.373Z ---- timer stop ----

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.0
    0
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.0
    0

Package Sidebar

Install

npm i action-timer

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

17.4 kB

Total Files

9

Last publish

Collaborators

  • yuto.technicalfeeder