示例 2: 时间间隔触发
()
| 66 | |
| 67 | // 示例 2: 时间间隔触发 |
| 68 | func demonstrateIntervalTrigger() { |
| 69 | fmt.Println("--- 示例 2: 时间间隔触发 ---") |
| 70 | |
| 71 | scheduler := core.NewScheduler(nil) |
| 72 | defer scheduler.Shutdown() |
| 73 | |
| 74 | // 每 500ms 执行一次 |
| 75 | taskID, err := scheduler.EveryInterval(500*time.Millisecond, func(ctx context.Context) error { |
| 76 | fmt.Printf("✓ 定时任务触发: %s\n", time.Now().Format("15:04:05.000")) |
| 77 | return nil |
| 78 | }) |
| 79 | |
| 80 | if err != nil { |
| 81 | log.Fatalf("创建定时任务失败: %v", err) |
| 82 | } |
| 83 | |
| 84 | fmt.Printf("定时任务已创建: %s\n", taskID) |
| 85 | fmt.Println("运行 2 秒...") |
| 86 | |
| 87 | // 运行 2 秒 |
| 88 | time.Sleep(2 * time.Second) |
| 89 | fmt.Println() |
| 90 | } |
| 91 | |
| 92 | // 示例 3: 步骤监听器 |
| 93 | func demonstrateStepListeners() { |
no test coverage detected