Package Exports
- quickinterval
- quickinterval/src/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (quickinterval) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
QuickInterval 
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 quickintervalExample 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(): voidpause()
Pauses the interval. If the interval is already paused or stopped, this method does nothing.
pause(): voidresume()
Resumes the interval. If the interval is already running, this method does nothing.
resume(): voidreset()
Stops and resets the interval.
reset(): voidisRunning()
Returns a boolean indicating whether the interval is currently running.
isRunning(): booleangetTimeElapsed()
Returns the elapsed time in milliseconds since the interval started.
getTimeElapsed(): numbersetDelay(delay)
Sets a new delay for the interval. If the interval is running, it is restarted with the new delay.
setDelay(delay: number): voidgetRemainingTime()
Returns the remaining time in milliseconds until the next interval callback.
getRemainingTime(): numberPrivate 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(): voidSponsors
- 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. 🙏😇
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.