Timer mechanism to place upper bound on rate of events.
npm install cooldown
This example reads lines of text from stdin. When 'spam' is entered, it will output 'spam' but only at a rate of once per 5 seconds. Entering 'reset' can reset the cooldown and allow 'spam' to succeed immediately after. Entering 'ready?' will display the state of the cooldown timer. The 'ready' event listener will automatically print when the timer is off cooldown.
var Cooldown = ;var readline = ;var rl = readline;// Set limit to 5svar cd = 5000;cd;rl;
A cooldown timer with two states:
- ready: available to be fired
- on-cooldown: fired and waiting to become ready again
- timeout: cooldown timeout duration (in ms)
Return true if the timer was ready (and puts it on-cooldown).
If timer is on-cooldown, reset it back to ready.
- noEmit: do not emit 'ready' event if timer was on-cooldown
Clear any timeouts and set timer to on-cooldown. It will never enter the 'ready' state unless it is reset.
Contains true if the timer is off cooldown and available to fire, else false.
Emitted whenever the timer comes back off cooldown.
- blocked: True if attempts were made to fire timer during cooldown period
Emitted whenever the timer goes on cooldown.