@wuyuchentr/cron-scheduler-lite
v1.0.0
Published
Lightweight cron scheduler with second-level precision. Zero dependencies, < 2 KB.
Maintainers
Readme
@wuyuchentr/cron-scheduler-lite
Lightweight cron scheduler — zero dependencies, < 2 KB. Supports second-level precision.
Install
npm install @wuyuchentr/cron-scheduler-liteUsage
const { schedule } = require('@wuyuchentr/cron-scheduler-lite');
// Run at 9:00 AM every day
const job = schedule('0 9 * * *', () => {
console.log('Time for morning standup!');
});
job.start();
// Later: stop the job
// job.stop();With seconds (6-field syntax)
// Every 5 seconds
schedule('*/5 * * * * *', () => poll());
// At 09:30:15 every day
schedule('15 30 9 * * *', () => backup());Cron expression reference (5-field)
┌───────── minute (0-59)
│ ┌───────── hour (0-23)
│ │ ┌───────── day of month (1-31)
│ │ │ ┌───────── month (1-12)
│ │ │ │ ┌───────── day of week (0-6, 0=Sun)
│ │ │ │ │
* * * * *Supported patterns
| Pattern | Description |
|---------|-------------|
| * | Every value |
| */n | Every n units |
| n | Exact value |
| n,m | List of values |
| n-m | Range |
Examples
schedule('0 9 * * 1-5', () => console.log('9 AM, weekdays'));
schedule('0 0 * * *', () => console.log('Midnight'));
schedule('*/15 * * * *', () => console.log('Every 15 minutes'));
schedule('0 */2 * * *', () => console.log('Every 2 hours'));
schedule('30 8 1 * *', () => console.log('8:30 AM on the 1st'));
schedule('* * * * * *', () => console.log('Every second'));How it works
- Parses the cron expression into typed fields (
all,step,list,range,exact) - Computes the exact next run time and schedules with
setTimeout - After each run, re-computes the next run (handles DST, clock drift)
- No polling, no
setInterval— pure event-driven
