# Run code on a schedule with CRON

> Register a function with CRON() and a five-field cron line, and know when the check runs and how to remove a job.

Framework: total5
Product: Total.js 5 (total5@0.0.19)
Language: en
Content-SHA256: 1e42e869db5a82463754c80c867472d8194d27b6f0858093ca51ce16e18ded54
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: total5-installed@total5@0.0.19
Review: not-recorded

`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.

```js controllers/cron.js
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);
}
```
