我目前有这段代码试图计算触发某个条件所用的时间。(伪):
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。
繁星coding
素胚勾勒不出你
白衣非少年
相关分类