循环倒计时

我目前有这段代码试图计算触发某个条件所用的时间。(伪):


timeDelay = 900000 // time.Microsecond

for {

   // if a certain something happens, start a counting (time)

   if (certainSomething) {

      startTime = time.Now();


      if !prevTime.IsZero() {

        // add the time elapsed time to timeTick

        diffTime = time.Since(prevTime)

        timeTick = timeTick + diffTime

      }


      prevTime = startTime

   }


   if (timeTick < timeDelay) { // lessThan()

      // still has not reached target count (time elapsed)

      fmt.Println("Not Yet")

      // we dont want to get to the end of the loop yet

      continue

   }


   if (timeTick > timeDelay) { // greaterThan()

      // has finally reached the target count (time elapsed)

      fmt.Println("Yes! Finally")

      // yes the delay has been reached lets reset the time

      // and if `thisHappened` is triggered again, lets start counting again

      timeTick = time.Duration(0 * time.Microsecond)

   }


   // function shouldn't be called if the elapsed amount

   //      of time required has not yet been reached

   iShouldOnlyBeCalledWhenDelayHasBeenReached();

}

我也使用这些作为辅助函数(实际代码)


func lessThan(diff time.Duration, upper int) bool {

    return diff < time.Duration(upper)*time.Microsecond && diff != 0

}


func greaterThan(diff time.Duration, upper int) bool {

    return diff > time.Duration(upper)*time.Microsecond

}

但是,我只是对自己的做法感到不舒服。我不应该数数吧?我应该倒计时......我只是很困惑,需要关于我应该使用什么方法的帮助。


我想要发生的事情:

1.发生timeDelay时从0 开始倒计时certainSomething。

2.iShouldOnlyBeCalledWhenDelayHasBeenReached在倒计时达到 0 之前不要调用

。 3. 这应该都发生在一个循环内,准确地说是服务器循环接收数据包。


我的问题:

1. 我应该怎么做才能实现倒计时风格?


谢谢,任何建议或示例代码都会有很大帮助。


注意:循环中还有其他函数。做其他事情。这是主循环。我来不及了Sleep。


慕姐4208626
浏览 386回答 3
3回答

繁星coding

您可以设置一个频道,让您知道何时超过了时间。这是播放示例它有一个额外的好处,即在 select 语句中,您可以将其他通道用于其他目的。例如,如果您正在此循环中执行其他工作,您还可以在 goroutine 中生成该工作并将结果发送回另一个通道。然后您timeDelay在其他工作完成之后或其他工作完成时退出,或者其他任何事情。package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "time")func main() {&nbsp; &nbsp; certainSomething := true // will cause time loop to repeat&nbsp; &nbsp; timeDelay := 900 * time.Millisecond // == 900000 * time.Microsecond&nbsp; &nbsp; var endTime <-chan time.Time // signal for when timer us up&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; // if a certain something happens, start a timer&nbsp; &nbsp; &nbsp; &nbsp; if certainSomething && endTime == nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endTime = time.After(timeDelay)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; select {&nbsp; &nbsp; &nbsp; &nbsp; case <-endTime:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Yes Finally!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endTime = nil&nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("not yet")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time.Sleep(50 * time.Millisecond) // simulate work&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // function shouldn't be called if the elapsed amount&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; of time required has not yet been reached&nbsp; &nbsp; &nbsp; &nbsp; iShouldOnlyBeCalledWhenDelayHasBeenReached() // this could also just be moved to the <- endtime block above&nbsp; &nbsp; }}func iShouldOnlyBeCalledWhenDelayHasBeenReached() {&nbsp; &nbsp; fmt.Println("I've been called")}

素胚勾勒不出你

由于这是您所说的游戏,因此在我看来您:想做X量的工作如果工作时间超过delay数量,停止工作并再次循环如果是这种情况,那么您有一些选择和模式。时间。想到之后。我喜欢time.Afterselect 语句中的简洁。不需要通道或 goroutine 来处理它。这种模式还有一个额外的好处,那就是在你的主要游戏逻辑中使用 goroutines。在玩:http : //play.golang.org/p/FIiUJ0CHZz更改time.After()以查看它的实际效果。func main() {&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; // creating a new channel on each loop will allow the&nbsp; &nbsp; &nbsp; &nbsp; // GC to collect any game stuff that completes after the timeout.&nbsp; &nbsp; &nbsp; &nbsp; done := make(chan int)&nbsp; &nbsp; &nbsp; &nbsp; // main game func, doing your other stuff.&nbsp; &nbsp; &nbsp; &nbsp; // since this is in a goroutine, it won't block&nbsp; &nbsp; &nbsp; &nbsp; // and we can check the state in the select.&nbsp; &nbsp; &nbsp; &nbsp; go func(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // complicated logic here!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // you must issue an "I am done!"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; done <- 1&nbsp; &nbsp; &nbsp; &nbsp; }()&nbsp; &nbsp; &nbsp; &nbsp; // this will block the for loop until a timeout occurs&nbsp; &nbsp; &nbsp; &nbsp; select {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case <- done:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case <- time.After(1000 * time.Nanosecond):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iShouldOnlyBeCalledWhenDelayHasBeenReached()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

白衣非少年

如果你不能让常规睡眠,你应该使用time.Time.Add和time.Time.After(或time.Time.Before)函数。沿着这些路线的东西package main&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;import "time"&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;func main() {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; var d = 1000 * time.Microsecond&nbsp; &nbsp; var t = time.Now().Add(d)&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if time.Now().Before(t) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; do_something()&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}或者驱逐您必须在可以睡眠的例程中运行一段时间后的方法:package main&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;import "time"&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;func main() {&nbsp; &nbsp; var d = 1000 * time.Microsecond&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; time.Sleep(d)&nbsp; &nbsp; &nbsp; &nbsp; do_something()&nbsp; &nbsp; }()}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go