Dynamically change ticker interval

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?

+7
source share
3 answers

After answering @fzerorubigd, but a little more complete.

, range , range , , ( : http://play.golang.org/p/yZvrgURz4o)

for - select. :

http://play.golang.org/p/3uJrAIhnTQ

package main

import (
    "time"
    "log"
    "fmt"
)

func main() {
    start_interval := float64(1000)
    quit := make(chan bool)

    go func(){
        ticker := time.NewTicker(time.Duration(start_interval) * time.Millisecond)
        counter := 1.0

        for {
            select {
            case <-ticker.C:
                log.Println("ticker accelerating to " + fmt.Sprint(start_interval/counter) + " ms")
                ticker.Stop()
                ticker = time.NewTicker(time.Duration(start_interval/counter) * time.Millisecond)
                counter++
            case <-quit:
                ticker.Stop()
                log.Println("..ticker stopped!")
                return
            }
        }
    }()

    time.Sleep(5 * time.Second)

    log.Println("stopping ticker...")
    quit<-true

    time.Sleep(500 * time.Millisecond) // just to see quit messages
}
+3

, "" . , :

package main

import (
    "fmt"
    "log"
    "time"
)

func main() {
    interval := float64(1000)

    ticker := time.NewTicker(time.Duration(interval) * time.Millisecond)
    go func() {
        counter := 1.0
        for {
            select {
            case <-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()
}
+1

:

https://play.golang.org/p/wyOTVxUW5Xj

package main

import (
    "fmt"
    "log"
    "time"
)

func main() {
    startInterval := float64(1000)
    quit := make(chan bool)

    go func() {
        counter := 1.0
        for {
            select {
            case <-time.After(time.Duration(startInterval/counter) * time.Millisecond):
                log.Println("ticker accelerating to " + fmt.Sprint(startInterval/counter) + " ms")
                counter++
            case <-quit:
                log.Println("..ticker stopped!")
                return
            }
        }
    }()

    time.Sleep(5 * time.Second)
    log.Println("stopping ticker...")
    quit <- true
    time.Sleep(500 * time.Millisecond) // just to see quit messages
}
0

All Articles