-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.go
49 lines (44 loc) · 1015 Bytes
/
timer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package gosocket
import "time"
// Timer is a utility class for the framework
type Timer struct {
ticker *time.Ticker
tickerDone chan bool
}
func (timer *Timer) Stop() {
//Validate timer.ticker first to avoid accessing a closed channel
//防止channel已关闭
if timer.ticker != nil {
timer.tickerDone <- true
}
}
type Callback func()
func NewTimer(interval time.Duration, callback Callback) *Timer {
timer := &Timer{
ticker: time.NewTicker(interval),
tickerDone: make(chan bool),
}
go func() {
defer func() {
//Stop the timer once the loop is over
//循环停止后再停止计时器
if timer.ticker != nil {
timer.ticker.Stop()
}
timer.ticker = nil
close(timer.tickerDone)
}()
//This label is essential here. Or else the break can only break out of select block
//必须通过Label,否则break只会跳出select
Loop:
for {
select {
case <-timer.ticker.C:
callback()
case <-timer.tickerDone:
break Loop
}
}
}()
return timer
}