1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// # Returns when your scheduled val should run
// This is a hacky way to get cron-like scheduled vals
// until we build them properly into Val Town.
// Returns `true` when your function should run
// Example: https://val.town/v/stevekrouse.sendDailyJoke
// Test: https://val.town/v/stevekrouse.exampleCronDay
export async function cron({
expression,
lastRunAt,
timezone,
_debugNow,
}: {
expression: string;
lastRunAt: Date;
timezone?: string;
_debugNow?: Date;
}) {
const { default: parser } = await import("npm:cron-parser");
const interval = parser.parseExpression(expression, {
tz: timezone,
currentDate: _debugNow,
});
// If the last time it was supposed to run is *after*
// the last time it ran, return `true` (meaning: run it now!)
return interval.prev().toDate() >= lastRunAt;
}
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.