Facebook Pixel

What is the difference between cron and setInterval?

Cron uses human-readable expressions (0 0 * * * for daily at midnight) and can handle complex schedules (weekdays at 9 AM). setInterval uses milliseconds and is lost on restart. Use cron for daily/weekly tasks, setInterval for short intervals.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in What Are Cron Jobs and Why Use Them in Node.js Scheduled Tasks Guide

A cron job is a scheduled task that runs automatically at fixed intervals using a cron expression (minute hour day month weekday). Examples: 0 0 * * * (daily at midnight), */15 * * * * (every 15 minutes), 0 9 * * 1-5 (weekdays at 9 AM). Used for cleanup, notifications, backups, and reports.

Install node-cron (npm install node-cron), then use cron.schedule('0 0 * * *', () => { ... }) to run a task daily at midnight. The cron expression specifies when to run, and the callback is the task to execute.

Email notifications (daily digests), data cleanup (delete old sessions, expired tokens), report generation (daily/weekly analytics), database backups, cache warming, subscription renewal checks, feed updates, and log rotation.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.