代码拉取完成,页面将自动刷新
package timer
import (
"errors"
"gitee.com/h79/goutils/common"
"time"
)
var (
TimeoutError = errors.New("operation timed out")
)
type CallbackFunc func(interface{}) bool
// Ticker
// 定时调用
func Ticker(tick time.Duration, fun CallbackFunc, param interface{},
funcDefer CallbackFunc, paramDefer interface{}) {
Delay(0, tick, fun, param, funcDefer, paramDefer)
}
func Delay(delay, tick time.Duration, fun CallbackFunc, param interface{},
funcDefer CallbackFunc, paramDefer interface{}) {
if fun == nil {
return
}
go func() {
defer common.Recover()
defer func() {
if funcDefer != nil {
funcDefer(paramDefer)
}
}()
if delay > 0 {
time.Sleep(delay)
if fun(param) {
return
}
}
ticker := time.NewTicker(tick)
for {
select {
case _ = <-ticker.C:
if fun(param) {
return
}
}
}
}()
}
func Call(timeout time.Duration, fn func() (interface{}, error)) (interface{}, error) {
if timeout > 0 {
resultCh := make(chan *resultWithError, 1)
go func() {
defer common.Recover()
result, err := fn()
resultCh <- &resultWithError{result, err}
}()
select {
case <-time.After(timeout):
return nil, TimeoutError
case rwe := <-resultCh:
return rwe.result, rwe.err
}
}
return fn()
}
type resultWithError struct {
result interface{}
err error
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。