diff --git a/examples/tickers/tickers.go b/examples/tickers/tickers.go index 110a192..1d2a8e5 100644 --- a/examples/tickers/tickers.go +++ b/examples/tickers/tickers.go @@ -16,9 +16,16 @@ func main() { // `range` builtin on the channel to iterate over // the values as they arrive every 500ms. ticker := time.NewTicker(500 * time.Millisecond) + done := make(chan bool) + go func() { - for t := range ticker.C { - fmt.Println("Tick at", t) + for { + select { + case <-done: + return + case t := <-ticker.C: + fmt.Println("Tick at", t) + } } }() @@ -27,5 +34,6 @@ func main() { // channel. We'll stop ours after 1600ms. time.Sleep(1600 * time.Millisecond) ticker.Stop() + done <- true fmt.Println("Ticker stopped") } diff --git a/examples/tickers/tickers.hash b/examples/tickers/tickers.hash index 0404929..28a173e 100644 --- a/examples/tickers/tickers.hash +++ b/examples/tickers/tickers.hash @@ -1,2 +1,2 @@ -7dc6447323f493f72aa70952bf3e3f2c6156f82f -Rgc_UDvHv6a +c83f34821c69d156713919a42c73ec9f58560f72 +IUmkvvXL5Ok diff --git a/public/tickers b/public/tickers index 2210a6a..8bb9280 100644 --- a/public/tickers +++ b/public/tickers @@ -45,7 +45,7 @@ periodically until we stop it.
package main
ticker := time.NewTicker(500 * time.Millisecond)
- go func() {
- for t := range ticker.C {
- fmt.Println("Tick at", t)
+ done := make(chan bool)
+
go func() {
+ for {
+ select {
+ case <-done:
+ return
+ case t := <-ticker.C:
+ fmt.Println("Tick at", t)
+ }
}
}()
time.Sleep(1600 * time.Millisecond)
ticker.Stop()
+ done <- true
fmt.Println("Ticker stopped")
}