You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
668 B
Go
46 lines
668 B
Go
//icomsys callback timer
|
|
package icscbtimer
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
type IcsCBTimerFunc func(t *IcsCBTimer)
|
|
|
|
type IcsCBTimer struct {
|
|
ticker *time.Ticker
|
|
d time.Duration
|
|
cbfunc IcsCBTimerFunc
|
|
}
|
|
|
|
func NewCBTimer(d time.Duration, callbackfunc IcsCBTimerFunc) *IcsCBTimer {
|
|
if d <= 0 || callbackfunc == nil {
|
|
return nil
|
|
}
|
|
|
|
it := IcsCBTimer{d: d, cbfunc: callbackfunc}
|
|
|
|
return &it
|
|
}
|
|
|
|
func (t IcsCBTimer) GetTick() <-chan time.Time {
|
|
return t.ticker.C
|
|
}
|
|
|
|
func (t *IcsCBTimer) Start() {
|
|
if t == nil {
|
|
return
|
|
}
|
|
|
|
t.ticker = time.NewTicker(t.d)
|
|
go t.cbfunc(t)
|
|
}
|
|
|
|
func (t *IcsCBTimer) Stop() {
|
|
if t == nil || t.ticker == nil {
|
|
return
|
|
}
|
|
|
|
t.ticker.Stop()
|
|
}
|