package callticker import ( "time" ) type CallTicker struct { manualTrigger chan interface{} function func(...interface{}) interface{} ticker *time.Ticker duration time.Duration } func NewCallTicker(d time.Duration, function func(...interface{}) interface{}) *CallTicker { ticker := &CallTicker{ manualTrigger: make(chan interface{}), function: function, ticker: time.NewTicker(d), duration: d, } return ticker } func (c *CallTicker) Stop() { c.ticker.Stop() } func (c *CallTicker) Reset(d time.Duration) { c.ticker.Reset(d) } func (c *CallTicker) Trigger() { c.manualTrigger <- true c.Reset(c.duration) } func (c *CallTicker) Run() { for { select { case _ = <-c.ticker.C: c.function() case _ = <-c.manualTrigger: c.function() } } }