cron-and-intervals
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCron / Interval Vals
Cron / Interval Vals
Interval vals () run on a recurring schedule defined by a cron expression. Use them for polling external APIs, sending reminders, running cleanups, generating reports, or any work that should happen on a clock rather than in response to a request.
fileType: "interval"Interval val()按照 cron 表达式定义的重复计划运行。适用于轮询外部 API、发送提醒、执行清理、生成报告,或任何应按时间执行而非响应请求的工作。
fileType: "interval"Basic handler
基本 handler
ts
// Learn more: https://docs.val.town/vals/cron/
export default async function (interval: Interval) {
// interval.lastRunAt: Date | undefined
console.log(interval);
}The file must have an — for the handler.
exportexport defaultts
// Learn more: https://docs.val.town/vals/cron/
export default async function (interval: Interval) {
// interval.lastRunAt: Date | undefined
console.log(interval);
}文件必须包含 ——handler 使用 。
exportexport defaultTimezone
时区
Cron expressions run in UTC. Convert any human-readable schedule (e.g. "9am Eastern") to UTC before writing the cron expression. Daylight savings is not handled — pick a UTC time that's close enough year-round.
Cron 表达式以 UTC 运行。 编写 cron 表达式之前,将任何以人类可读方式表达的日程(例如 "9am Eastern")转换为 UTC。系统不处理夏令时——请选择一个全年都足够接近的 UTC 时间。
The lastRunAt
pattern
lastRunAtlastRunAt
模式
lastRunAtinterval.lastRunAtundefinedts
export default async function (interval: Interval) {
const since = interval.lastRunAt ?? new Date(Date.now() - 24 * 60 * 60 * 1000);
const newItems = await fetchItemsSince(since);
for (const item of newItems) {
await handle(item);
}
}This makes the val idempotent against missed runs and avoids reprocessing.
interval.lastRunAtundefinedts
export default async function (interval: Interval) {
const since = interval.lastRunAt ?? new Date(Date.now() - 24 * 60 * 60 * 1000);
const newItems = await fetchItemsSince(since);
for (const item of newItems) {
await handle(item);
}
}这样即使错过某次运行,也能让 val 保持幂等,并避免重复处理。
Reading and updating the schedule
读取和更新计划
- — fetch the current cron expression and active state of an interval file.
read_interval_settings - — change the cron expression or pause/resume an interval.
write_interval_settings
- ——获取 interval 文件当前的 cron 表达式和启用状态。
read_interval_settings - ——修改 cron 表达式或暂停/恢复 interval。
write_interval_settings
When to skip a template
何时无需使用模板
For simple scheduled jobs, create a new val with a single -type file directly — no template needed. Templates are for more complex shapes (dashboards, AI agents, webhook + UI combos).
interval对于简单的定时作业,直接创建一个仅包含单个 类型文件的新 val 即可,无需使用模板。模板适用于更复杂的形态(仪表盘、AI agent、webhook + UI 组合)。
intervalVerifying changes
验证更改
After editing an interval val, use to invoke the handler manually instead of waiting for the next scheduled run.
run_file编辑 interval val 后,使用 手动调用 handler,而不必等待下一次计划运行。
run_file