I would like to dynamically change the interval between tickers.
I recorded an example to show you how I did it. My use case is something other than an accelerometer, but I hope this gives you an idea.
http://play.golang.org/p/6ANFnoE6pA
package main
import (
"time"
"log"
"fmt"
)
func main() {
interval := float64(1000)
ticker := time.NewTicker(time.Duration(interval) * time.Millisecond)
go func(){
counter := 1.0
for range ticker.C {
log.Println("ticker accelerating to " + fmt.Sprint(interval/counter) + " ms")
ticker = time.NewTicker(time.Duration(interval/counter) * time.Millisecond)
counter++
}
log.Println("stopped")
}()
time.Sleep(5 * time.Second)
log.Println("stopping ticker")
ticker.Stop()
}
What is wrong with the fact that the ticker will always “tick” every second, and it will not accelerate ... Any ideas?
source
share