Skip to content

Instantly share code, notes, and snippets.

@nwillc
Last active February 14, 2021 01:38
Show Gist options
  • Save nwillc/6d5e7a359750397b43d26734e8562888 to your computer and use it in GitHub Desktop.
Save nwillc/6d5e7a359750397b43d26734e8562888 to your computer and use it in GitHub Desktop.
package util
import (
"time"
)
type StopChannel chan bool
// RepeatUntilStopped creates a goroutine that loops, waiting the delay provided and
// then calling the function provided. The loop can be stopped by sending a value to
// returned StopChannel.
func RepeatUntilStopped(delay time.Duration, f func()) StopChannel {
control := make(chan bool, 1)
go func() {
for {
select {
case <-control:
return
case <-time.After(delay):
}
f()
}
}()
return control
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment