Easy Cron JS

A simple cron/task scheduler for JavaScript, making it really easy to run multiple recurring tasks with different intervals. It’s easy to use and has no dependencies.

The project came about from my need of a lightweight way to run periodic housekeeping tasks in a node/express application.

Installation

# Add easy-cronjs to your project.
npm install @headwall/easy-cronjs

Example usage

cron = require('@headwall/easy-cronjs');

// Enable diagnostics (optional).
// cron.enableDiagnostics = true;

// Do something every 1000ms (one second interval)
cron.addJob('My Cron Job', 1000, () => {
	console.log('tick');
});

// Do something else every ten minutes
cron.addJob('Another Task', 1000*60*10, () => {
	console.log('tick');
});

// Start easy-cronjs
cron.start();

//
// Your application logic...
//

// Disable one of the jobs
cron.disableJob('My Cron Job');

// ...
// ...
// ...

// Re-enable the job
cron.enableJob('My Cron Job');

// ...
// ...
// ...


// Stop easy-cronjs
cron.stop();

// All done
console.log('end');

Leave a comment