HomeServer/lualib-src/Server-main/Common/Timer.lua

35 lines
1.2 KiB
Lua
Raw Normal View History

2024-11-20 15:41:09 +08:00
local skynet = require "skynet"
local oo = require "Class"
local log = require "Log"
local dataType = require "DataType"
local Timer = oo.class()
Timer.list = {}
Timer.Status_Start = 1 --运行
Timer.Status_Stop = 2 --停止
--新增Timer
function Timer:Add( type , interval , callFunc , parent )
if not self.list[ type ] then
self.list[ type ] = {}
end
self.list[ type ].status = self.Status_Start --状态
self.list[ type ].callFunc = callFunc --回调函数
self.list[ type ].parent = parent --回调参数
self.list[ type ].interval = interval --间隔时间
self.list[ type ].nextFreshTime = skynet.GetTime() + interval --下一次刷新时间
end
--刷新Timer
function Timer:Refresh()
for k, v in pairs( self.list ) do
if self.Status_Start == v.status and skynet.GetTime() >= v.nextFreshTime then
v.callFunc( v.parent )
v.nextFreshTime = skynet.GetTime() + v.interval --下一次刷新时间
end
end
end
skynet.server.timer = Timer
return Timer