使用 cloudflare 定时器
使用 cloudflare 设置定时器,执行一些需要定时执行的脚本,也可以定时部署 cloudflare page。
创建项目
-
-
点击create application
-
选择workers,默认已经选择
-
点击create worker
自动生成的代码
export default {
async scheduled(event, env, ctx) {
// 这里改成想要执行的代码
ctx.waitUntil(fetch(env.url, {method: "POST"}));
},
};
- 部署(Deploy)
设置定时器
- 部署之后进入详情页
- 点击设置 Settings - Triggers - Add Cron Trigger
设置触发时间
环境变量
- 点击设置 - Variables
- 找到 Environment Variables,点击 Edit variables
添加变量
可以选择加密或者不加密,选择加密在读取时不需要解密,只是当前页面不可见。
变量名可在代码中读取,比如我这里的 url
可以用 env.url
读取
注意事项
- 触发时间为国际时间