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

1.1.3 • Public • Published

QuickInterval NPM version

QuickInterval is a flexible and user-friendly package for creating intervals that execute a specified callback function. It offers useful features such as pause, resume, reset, and dynamic interval delay adjustment. The class is designed to work within a Node.js environment, making it a reliable choice for your interval needs.

Features

  • Less RAM consumption because it depends on the frequency of Timeout Nodejs, which saves more memory than Interval NodeJS
  • Start and stop the interval execution
  • Pause and resume the interval execution
  • Reset the interval and start again
  • Change the interval delay dynamically
  • Get the elapsed time of the interval

Installation

Install with npm / yarn / pnpm:

npm add quickinterval
yarn add quickinterval
pnpm add quickinterval

Example Usage

import { Interval } from 'quickinterval';

const interval = new Interval(() => console.log("Interval callback"), 1000);

interval.start(); // starts the interval

setTimeout(() => {
  interval.pause(); // pauses the interval after 5 seconds
}, 5000);

setTimeout(() => {
  interval.resume(); // resumes the interval after 10 seconds
}, 10000);

setTimeout(() => {
  interval.reset(); // resets the interval after 15 seconds
}, 15000);

API

Interval Class

The Interval class is a custom implementation of an interval that provides functionality similar to the built-in setInterval method in JavaScript. However, this implementation is more efficient in terms of memory usage and provides additional methods for more flexibility.

Constructor

The Interval class constructor takes two parameters, a callback function and a delay in milliseconds. The callback function is called repeatedly after the specified delay until the interval is stopped or reset.

constructor(callback: () => void, delay: number)

Methods

start()

Starts the interval. If the interval is already running, this method does nothing.

start(): void

pause()

Pauses the interval. If the interval is already paused or stopped, this method does nothing.

pause(): void

resume()

Resumes the interval. If the interval is already running, this method does nothing.

resume(): void

reset()

Stops and resets the interval.

reset(): void

isRunning()

Returns a boolean indicating whether the interval is currently running.

isRunning(): boolean

getTimeElapsed()

Returns the elapsed time in milliseconds since the interval started.

getTimeElapsed(): number

setDelay(delay)

Sets a new delay for the interval. If the interval is running, it is restarted with the new delay.

setDelay(delay: number): void

getRemainingTime()

Returns the remaining time in milliseconds until the next interval callback.

getRemainingTime(): number

Private Method

scheduleNext()

Schedules the next interval callback and calculates the remaining time until the next callback. This method is called recursively as long as the interval is running.

private scheduleNext(): void

Sponsors

  • Love what I do? Send me some coffee !?
  • Can't send coffees? Your support will help me to continue working on open-source projects like this. 🙏😇

Powered by Shuruhatik

Help

If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to join our official Discord Server .

License

Refer to the LICENSE file.

Package Sidebar

Install

npm i quickinterval

Weekly Downloads

13

Version

1.1.3

License

BSD-2-Clause

Unpacked Size

13.1 kB

Total Files

8

Last publish

Collaborators

  • shuruhatik