Skip to content

Timer ​

继承自:

描述 ​

定时器节点:Timer定时器节点允许指定一个lua回调方法在一定时间后执行。

属性 ​

LuaFunction  Callback
lua回调方法
double  Delay
首次延迟执行的时间
bool  Loop
是否循环执行
double  Interval
计时间隔时间

函数 ​

void  Start ()
开始执行
void  Pause ()
暂停。需要在开始执行后调用
void  Resume ()
恢复。需要在暂停后调用
void  Stop ()
停止。需要在开始执行后调用
获取定时器运行状态
void  StartEx (double delay, bool loop, double interval, LuaFunction cb)
开始执行。附带初始化的参数此服务器中可以容纳的最大玩家数量

代码示例 ​

lua
local a = 0
local timer = SandboxNode.New("Timer") -- 创建定时器节点
timer.Delay = 1 -- 延迟多少秒开始
timer.Loop = true -- 是否循环
timer.Interval = 2 -- 循环间隔多少秒
timer.Callback = function() -- 回调方法
    a = a + 1
    print("timer : ", timer, " a=", a)
    if a == 4 then
        print("timer pause")
        timer:Pause() -- 暂停定时器,只有在定时器运行期间有效
        wait(4)
        print("timer resume")
        timer:Resume() -- 恢复定时器,只有在定时器运行暂停期间有效
    end
end
timer:Start()

-- 一次性传入参数,并且开始定时器
--timer:StartEx(3, true, 3, function() a = a + 1; print("timer ex : a=", a) end)
print("timer start")