From 947f17089ad4bf92820d2a2ff96dad29bb7284f8 Mon Sep 17 00:00:00 2001 From: MarvinJWendt Date: Sun, 20 Aug 2023 19:33:38 +0200 Subject: [PATCH] feat: `.Every` can now stop itself --- examples_test.go | 3 ++- schedule.go | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/examples_test.go b/examples_test.go index 617140b..1e2dba0 100644 --- a/examples_test.go +++ b/examples_test.go @@ -28,8 +28,9 @@ func ExampleAt() { } func ExampleEvery() { - task := schedule.Every(time.Second, func() { + task := schedule.Every(time.Second, func() bool { fmt.Println("1 second is over!") + return true // return false to stop the task }) fmt.Println("Some stuff happening...") diff --git a/schedule.go b/schedule.go index 1ab1c83..635d29d 100644 --- a/schedule.go +++ b/schedule.go @@ -91,9 +91,9 @@ func At(t time.Time, task func()) *Task { return scheduler } -// Every executes the task in the given interval. +// Every executes the task in the given interval, as long as the task function returns true. // The function is non-blocking. If you want to wait for the task to be executed, use the Task.Wait method. -func Every(interval time.Duration, task func()) *Task { +func Every(interval time.Duration, task func() bool) *Task { scheduler := newTask() scheduler.nextExecution = time.Now().Add(interval)