I would like to change my ticker interval dynamically.
I've written down an example to show you how I did. My use case is something else than an "accelerometer" but I hope that it 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 is that the ticker will always "tick" every seconds and it doesn't accelerate... Any idea?