kleinTodo/common/scheduler.go

26 lines
326 B
Go
Raw Permalink Normal View History

2025-07-26 23:31:00 +02:00
package common
import (
"time"
)
func Schedule(seconds time.Duration, task func()) (stop func()) {
ticker := time.NewTicker(seconds)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
task()
case <-quit:
ticker.Stop()
return
}
}
}()
return func() {
close(quit)
}
}