Run code on a schedule with CRON
CRON(line, fn) runs fn(date) whenever the current time matches line. The line has five fields, as in Unix cron: minute hour day-of-month month day-of-week. Each field accepts * (any value), 1,5,9 (a list), 1-5 (a range) or an exact number. Day names such as mon or fri work in the last field.
Write the values as a list, 0,15,30,45: it matches exactly the times you name.
Total.js checks the registered jobs once a minute, so the finest resolution is one minute. CRON() returns an object: job.remove() unregisters it and job.check(date) tells you whether a date matches the line, which is also the easiest way to test a schedule without waiting for it.
exports.install = function() {
ROUTE('GET /cron/matches/', matches);
ROUTE('GET /cron/runs/', runs);
};
var counter = { runs: 0 };
// Every 15 minutes, from Monday to Friday, between 09:00 and 17:59
var job = CRON('0,15,30,45 9-17 * * 1-5', function(date) {
counter.runs++;
});
function matches($) {
var dates = {
monday_0930: new Date(2026, 0, 5, 9, 30),
monday_0931: new Date(2026, 0, 5, 9, 31),
monday_1830: new Date(2026, 0, 5, 18, 30),
sunday_0930: new Date(2026, 0, 4, 9, 30)
};
var output = {};
for (var key in dates)
output[key] = job.check(dates[key]);
$.json(output);
}
function runs($) {
$.json(counter);
}