代码拉取完成,页面将自动刷新
package timer
/*
timer for few func call, should not use most sence for performance.
note: 20200108 by stefan.
*/
import (
"github.com/Peakchen/xgameCommon/akLog"
"reflect"
"time"
)
/*
timer:
func
params
interval
count
*/
type TAokoTimer struct {
Func refect.Value
Params []reflect.Value
Interval int
Count int
curSec int //after call, then reset to 0
}
type TAokoTimerMgr struct {
timers []*TAokoTimer
}
var (
_timerMgr *TAokoTimerMgr
)
func init() {
_timerMgr = &TAokoTimerMgr{
timers: []*TAokoTimer{},
}
}
func Run() {
var sw sync.WaitGroup
sw.Add(1)
go timerloop()
}
func timerloop() {
tick := time.NewTicker(time.Duration(1) * time.Second)
var (
removeTimers = []int{}
)
for {
select {
case <-tick.C:
for idx, timercall := range _timerMgr.timers {
if timercall.curSec >= timercall.Interval && timercall.Count > 0 {
timercall.Func.Call(timercall.Params)
timercall.Count--
timercall.curSec = 0
} else {
timercall.curSec++
}
if timercall.Count == 0 {
removeTimers = append(removeTimers, idx)
}
}
for _, timeridx := range removeTimers {
_timerMgr.timers = append(_timerMgr.timers, _timerMgr.timers[:timeridx], _timerMgr.timers[timeridx+1:]...)
}
// clean data
removeTimers = []int{}
}
}
}
func (this *TAokoTimerMgr) register(fun interface{}, interval int, count int, args ...interface{}) {
var params = []reflect.Value{}
for _, arg := range args {
params = append(params, reflect.ValueOf(arg))
}
timer := &TAokoTimer{
Func: reflect.ValueOf(fun),
Params: params,
Interval: interval,
Count: count,
}
if cstTimerUpLimit <= len(_timerMgr.timers) {
akLog.Error("timer list size limit arrives.")
return
}
_timerMgr.timers = append(_timerMgr.timers, timer)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。