dqa35710 2016-10-27 13:54 采纳率: 0%
浏览 8

Golang:转向窗罩

I want to operate my window coverings with my smartphone. Now every time I change the position on my phone a method

func main() {
OnUpdate(func(tPos int) {
     wc(tPos,cPos)
     cPos = tPos
  }
}

is called where tPos is an integer between 0 and 100 which is is target position. There also is a variable for the current position cPos. OnUpdate should call a function which either open or closes the window covering depending of the order relation between cPos and tPos. This function looks like this.

func wc(tPos int, cPos int){
    switch{
        case tPos == 0:
            log.Println("close")
        case tPos == 100:
            log.Println("open")
        case tPos > cPos:
            t := time.Duration( (tPos - cPos)*10*openTime)
            log.Println("open")
            time.Sleep( t  * time.Millisecond)
            log.Println("stop")
        case tPos < cPos:
            t := time.Duration( (cPos - tPos)*10*closeTime)
            log.Println("close")
            time.Sleep( t  * time.Millisecond)
            log.Println("stop")
    }
}

My problem now is that there should be some delay. I want that after OnUpdate is called there is a timer for like 3 seconds and then wc is called unless OnUpdate is called again during that 3 seconds.

But I don't know how to do this. Can someone tell me what is a good way to do that?

  • 写回答

1条回答 默认 最新

  • doty58493 2016-10-27 20:10
    关注

    Not completely sure about your meaning, but I'll give it a go anyway. What calls OnUpdate? Is that on you phone? Do you simply want to sleep before calling wc inside the callback you provide to OnUpdate?

    With that in mind please have a look at : https://play.golang.org/p/4vVpEEUcqg

    My understanding is that you want make sure wc is not called too often. The for/select statements in run makes sure that wc is only called once every 3 seconds at most.

    评论

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接