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

1.0.4 • Public • Published

Timer

This is a class that allows you to create a simple timer in JavaScript. You can use this class to execute a callback function periodically after a specified time interval.

Installation

To install the Timer you can use npm:

npm i sgb004-timer

Usage

To use it, simply create a new Timer instance passing in the callback function, interval in milliseconds, and optionally an initial delay:

const timer = new Timer(() => { console.log('¡Hola!'); }, 1000, 5000);

This will execute the callback function every second, after an initial 5 seconds delay.

Then to start the timer, call the start() method:

timer.start();

And to stop it, use stop():

timer.stop();

You can also restart the timer with reset():

timer.reset();

API

Constructor

  • callback - Function to execute on each interval.
  • time - (Optional) Interval in milliseconds.
  • delay - (Optional) Initial delay in milliseconds before starting.

Methods

  • start() - Starts the timer execution.
  • stop() - Stops the timer.
  • reset() - Restarts the timer.

Author

sgb004

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i sgb004-timer

Weekly Downloads

3

Version

1.0.4

License

MIT

Unpacked Size

6.75 kB

Total Files

6

Last publish

Collaborators

  • sgb_004