//icomsys callback timer package icscbtimer import ( "time" ) type IcsCBTimerFunc func(t *IcsCBTimer) type IcsCBTimer struct { ticker *time.Ticker d time.Duration cbfunc IcsCBTimerFunc STOP chan bool } 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) t.STOP = make(chan bool, 1) go t.cbfunc(t) } func (t *IcsCBTimer) Stop() { if t == nil || t.ticker == nil { return } t.STOP <- true t.ticker.Stop() }