Created
November 5, 2015 10:13
-
-
Save ckeyer/85160a8260ca45acf1e6 to your computer and use it in GitHub Desktop.
随机循环定时
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var period int64 = 5 | |
var next_period = time.Now() | |
var timer time.Duration | |
time.Sleep(time.Second * 1) | |
for { | |
if next_period.Before(time.Now()) { | |
next_period = time.Now() | |
} | |
timer = time.Duration(rand.Int63n(period)+1)*time.Second + (next_period.Sub(time.Now())) | |
select { | |
case <-time.After(timer): | |
func() { | |
fmt.Println("Print RUUUUUUUUUU: ", time.Now().Second()) | |
next_period = next_period.Add(time.Duration(period) * time.Second) | |
fmt.Println("next_per sec: ", next_period.Second()) | |
}() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment