本文介绍了动态更改动态时间间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动态更改自动收报机间隔。

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



  package main 

import(
time
log
fmt


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

ticker:= time.NewTicker(time.Duration(interval)* time.Millisecond)
去func(){
counter:= 1.0
范围ticker.C {
log.Println(ticker accelerating to+ fmt.Sprint(interval / counter)+ms)
ticker = time.NewTicker(time.Duration(interval / counter)* time.Millisecond)
计数器++

log.Println(已停止)
}()
time.Sleep(5 * time.Second)
log.Println )
ticker.Stop()
}

股票代码会每隔一秒滴答一次,它不会加速......任何想法?

解决方案

遵循@fzerorubigd的答案,但更完整一点。

如前所述,在这种情况下,我们不能使用范围,因为范围循环缓存要变的变量,然后它不能被覆盖(例如:)

然后,我们应该使用作为 - 选择组合循环。以下工作解决方案:

 包主

导入(
time
log
fmt


func main(){
start_interval:= float64(1000)
退出:= make(chan bool)
$ b $ 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!)
返回
}
}
}()

time.Sleep(5 * time.Second)

log.Println(stopping ...)
退出< -true

time.Sleep(500 * time.Millisecond)//仅查看退出消息
}
code>


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?

解决方案

Following the answer to @fzerorubigd but a little more complete.

As said before, we can't use the range for this case, because the range loop caches the variable to be lopped and then it can't be overwritten (example here: http://play.golang.org/p/yZvrgURz4o )

Then, we should use a for-select combination loop. Hereafter the working solution:

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
}

这篇关于动态更改动态时间间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 20:57