codecamp

xingo 定时器

1) 延迟执行

func (this *Server) CallLater(durations time.Duration, f func(v ...interface{}), args ...interface{}){
       delayTask := timer.NewTimer(durations, f, args)
       delayTask.Run()
}
示例:

func testTimer(args ...interface {}){
       logger.Info(fmt.Sprintf("%s-%d-%f", args[0], args[1], args[2]))
}
s.CallLater(5*time.Second, testTimer, "viphxin", 10009, 10.999)
//s 为xingo server ,服务将会延迟5秒之后执行testTimer


2) 定时执行

func (this *Server) CallWhen(ts string, f func(v ...interface{}), args ...interface{}){
       loc, err_loc := time.LoadLocation("Local")
       if err_loc != nil{
              logger.Error(err_loc)
              return
       }
       t, err := time.ParseInLocation("2006-01-02 15:04:05", ts, loc)
       now := time.Now()
       //logger.Info(t)
       //logger.Info(now)
       //logger.Info(now.Before(t) == true)
       if err == nil{
              if now.Before(t){
                     this.CallLater(t.Sub(now), f, args...)
              }else{
                     logger.Error("CallWhen time before now")
              }
       }else{
              logger.Error(err)
       }
}

示例:

s.CallWhen("2016-12-15 18:35:10", testTimer, "viphxin", 10009, 10.999)
//s 为xingo server ,服务将会在2016-12-15 18:35:10执行testTimer,注意时间必须是将来的时间


3) 循环执行

func (this *Server)CallLoop(durations time.Duration, f func(v ...interface{}), args ...interface{}){
       go func() {
              delayTask := timer.NewTimer(durations, f, args)
              for {
                     time.Sleep(delayTask.GetDurations())
                     delayTask.GetFunc().Call()
              }
       }()
}
示例:

s.CallLoop(5*time.Second, testTimer, "loop--viphxin", 10009, 10.999)
//s 为xingo server ,服务将会每隔5秒执行testTimer

xingo 集群服务器入门教程
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }