动态更改代码间隔

我想动态更改我的代码间隔。


我已经写下一个例子来向你展示我是如何做到的。我的用例不是“加速度计”,但我希望它能给你一个想法。


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()

}

有什么不对的是,自动收报机总是每秒钟“滴答”一下,而且不会加速……知道吗?


婷婷同学_
浏览 151回答 3
3回答

青春有我

遵循@fzerorubigd 的答案,但更完整一些。如前所述,我们不能在range这种情况下使用 ,因为range循环缓存要删除的变量,然后它不能被覆盖(例如:http : //play.golang.org/p/yZvrgURz4o)然后,我们应该使用for-select组合循环。此后的工作解决方案:http://play.golang.org/p/3uJrAIhnTQpackage mainimport (&nbsp; &nbsp; "time"&nbsp; &nbsp; "log"&nbsp; &nbsp; "fmt")func main() {&nbsp; &nbsp; start_interval := float64(1000)&nbsp; &nbsp; quit := make(chan bool)&nbsp; &nbsp; go func(){&nbsp; &nbsp; &nbsp; &nbsp; ticker := time.NewTicker(time.Duration(start_interval) * time.Millisecond)&nbsp; &nbsp; &nbsp; &nbsp; counter := 1.0&nbsp; &nbsp; &nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case <-ticker.C:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Println("ticker accelerating to " + fmt.Sprint(start_interval/counter) + " ms")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ticker.Stop()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ticker = time.NewTicker(time.Duration(start_interval/counter) * time.Millisecond)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter++&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case <-quit:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ticker.Stop()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Println("..ticker stopped!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }()&nbsp; &nbsp; time.Sleep(5 * time.Second)&nbsp; &nbsp; log.Println("stopping ticker...")&nbsp; &nbsp; quit<-true&nbsp; &nbsp; time.Sleep(500 * time.Millisecond) // just to see quit messages}

潇湘沐

正如 Nipun Talukdar 提到的,“for”捕获通道并使用相同的引用进行迭代。如果您像这样使用它,它会修复:package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "log"&nbsp; &nbsp; "time")func main() {&nbsp; &nbsp; interval := float64(1000)&nbsp; &nbsp; ticker := time.NewTicker(time.Duration(interval) * time.Millisecond)&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; counter := 1.0&nbsp; &nbsp; &nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case <-ticker.C:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Println("ticker accelerating to " + fmt.Sprint(interval/counter) + " ms")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ticker = time.NewTicker(time.Duration(interval/counter) * time.Millisecond)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter++&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; log.Println("stopped")&nbsp; &nbsp; }()&nbsp; &nbsp; time.Sleep(5 * time.Second)&nbsp; &nbsp; log.Println("stopping ticker")&nbsp; &nbsp; ticker.Stop()}

森林海

这就是为什么在 go1.15ticker.Reset中创建的原因,您不需要创建新的股票代码更新现有股票代码的持续时间ticker.Reset("new duration"),现在您不会有任何缓存问题package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "log"&nbsp; &nbsp; "time")func main() {&nbsp; &nbsp; interval := float64(1000)&nbsp; &nbsp; ticker := time.NewTicker(time.Duration(interval) * time.Millisecond)&nbsp; &nbsp; go func(){&nbsp; &nbsp; &nbsp; &nbsp; counter := 1.0&nbsp; &nbsp; &nbsp; &nbsp; for range ticker.C {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Println("ticker accelerating to " + fmt.Sprint(interval/counter) + " ms")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ticker.Reset(time.Duration(interval/counter) * time.Millisecond)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter++&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; log.Println("stopped")&nbsp; &nbsp; }()&nbsp; &nbsp; time.Sleep(5 * time.Second)&nbsp; &nbsp; log.Println("stopping ticker")&nbsp; &nbsp; ticker.Stop()}您的示例存在缓存问题的原因是,当您ticker使用*time.ticker结构重新分配变量时,您只需取消原始*time.ticker结构与ticker变量的链接,但循环仍处于原始代码通道中,您需要将新循环重新分配给新的time.ticker.c
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go