What is retimer?
The 'retimer' npm package is a utility for creating and managing timers that can be reset. It is useful for scenarios where you need to repeatedly reset a timer based on certain events or conditions.
What are retimer's main functionalities?
Creating a Timer
This feature allows you to create a timer that will execute a callback function after a specified delay. In this example, the timer will log 'Timer expired' after 1000 milliseconds.
const retimer = require('retimer');
const timer = retimer(() => {
console.log('Timer expired');
}, 1000);
Resetting a Timer
This feature allows you to reset the timer to a new delay. In this example, the timer is initially set to expire in 1000 milliseconds, but it is reset to expire in another 1000 milliseconds after 500 milliseconds have passed.
const retimer = require('retimer');
const timer = retimer(() => {
console.log('Timer expired');
}, 1000);
// Reset the timer to expire in another 1000 milliseconds
setTimeout(() => {
timer.reschedule(1000);
}, 500);
Clearing a Timer
This feature allows you to clear the timer before it expires. In this example, the timer is cleared after 500 milliseconds, so the 'Timer expired' message will not be logged.
const retimer = require('retimer');
const timer = retimer(() => {
console.log('Timer expired');
}, 1000);
// Clear the timer before it expires
setTimeout(() => {
timer.clear();
console.log('Timer cleared');
}, 500);
Other packages similar to retimer
node-schedule
The 'node-schedule' package is a flexible cron-like and not-cron-like job scheduler for Node.js. It allows you to schedule jobs using cron syntax or by specifying a specific date. Unlike 'retimer', which focuses on resetting timers, 'node-schedule' is more suited for scheduling recurring tasks.
cron
The 'cron' package provides a way to schedule jobs using cron syntax. It is useful for running tasks at specific intervals or times. While 'retimer' is designed for timers that can be reset, 'cron' is more focused on scheduling tasks based on time patterns.
later
The 'later' package is a library for describing recurring schedules and calculating future occurrences. It provides a more advanced scheduling mechanism compared to 'retimer', which is primarily focused on simple timer management.
retimer
reschedulable setTimeout for you node needs. This library is built for
building a keep alive functionality across a large numbers of
clients/sockets.
Rescheduling a 10000 functions 20 times with an interval of 50ms (see
bench.js
), with 100 repetitions:
benchSetTimeout*100
: 51867msbenchRetimer*100
: 34237ms
Install
npm install retimer --save
Example
var retimer = require('retimer')
var timer = retimer(function () {
throw new Error('this should never get called!')
}, 20)
setTimeout(function () {
timer.reschedule(50)
setTimeout(function () {
timer.clear()
}, 10)
}, 10)
API
retimer(callback, timeout, [...args])
Exactly like your beloved setTimeout
.
Returns a Retimer object
timer.reschedule(timeout)
Reschedule the timer, if the specified timeout comes after the
original timeout.
Returns true if successful, false otherwise
timer.clear()
Clear the timer, like your beloved clearTimeout
.
How it works
Timers are stored in a Linked List in node.js, if you create a lot of
timers this Linked List becomes massive which makes removing a timer an expensive operation.
Retimer let the old timer run at its time, and schedule a new one accordingly.
License
MIT