set-alarm
v2.0.1
Published
Call a Function on a given Date
Downloads
10
Maintainers
Readme
:alarm_clock: set-alarm
Call a Function
on a given Date
.
The API tries to mimic the setTimeout
API as best as possible.
See docs.
import {
setAlarm,
clearAlarm,
Alarm
} from 'set-alarm';
const firstOfApril2021AtNoonZuluTime = new Date('2021-04-01T12:00Z');
// Set an alarm
const alarm = setAlarm(() => {
console.debug(new Date() - firstOfApril2021AtNoonZuluTime);
}, firstOfApril2021AtNoonZuluTime);
alarm instanceof Alarm; // true
// Two scenarios:
// - if date is in the future: should trigger at or just after the date, logging a small nonnegative number
// - if date is in the past: will trigger anyway logging a possibly large positive number
// It is up to you to ignore alarms for past dates (similar to `setTimeout(..., -2389324)`).
// OR Unenroll the callback (and the alarm will not trigger)
clearAlarm(alarm); // undefined