Task schedule 是一坨屎

发布: (2026年4月30日 GMT+8 16:06)
2 分钟阅读
原文: Dev.to

Source: Dev.to

概览

在特定时间运行你的 TypeScript 文件。该库支持三种调度方式:

  • interval – 按设定间隔重复运行
  • atTime – 每天在特定时间运行一次
  • inRange – 在定义的时间范围内运行

详细选项请参见 api.ts

创建脚本

  1. scripts 文件夹中创建一个以 .sche.ts 为后缀的 TypeScript 文件。
  2. 导入 API 并实例化它。
// import API from api.ts
import { API } from './api.ts';

// instantiate API
const timer = new API();

示例

Interval

每 5 分钟运行一次函数:

timer.interval({
    min: 5,
    func: () => {
        console.log('hello world');
    }
});

在特定时间

每天在 10:00 运行一次函数:

timer.atTime({
    time: '10:00',
    func: () => {
        console.log('hello world');
    }
});

随系统启动

要在系统启动时自动运行脚本,创建一个快捷方式(Alt + 拖拽),并将其放入 shell:startup 文件夹。

使用包

如果脚本需要额外的包,先初始化一个 npm 项目并使用 npmbun 安装依赖。

npm init -y
npm install 
# or with bun
bun init
bun add 

配置

你可以在 api.ts 中调整 checkInterval 设置,以改变调度器检查待执行任务的频率。

0 浏览
Back to Blog

相关文章

阅读更多 »