|
- -- 定时器表,存储所有定时任务
- timer = {}
- timer._timers = {}
- function timer.schedule(delay, callback) -- 加入定时器和函数到表
- table.insert(timer._timers, {time = os.clock() + delay, callback = callback})
- end
- -- 更新定时器,检查是否有定时任务到期
- function timer.update()
- new_dd = (new_dd or 0) + 1
- SL:print("执行定时器"..new_dd)
- local now = os.clock()
- for i = #timer._timers, 1, -1 do
- local t = timer._timers[i]
- SL:print("检测定时器时间:",now , t.time )
- if now >= t.time then
- SL:print("到时间了!")
- t.callback()
- table.remove(timer._timers, i) -- 移除已完成的定时任务<!--EndFragment-->
- end
- end
- end
- -- 清除所有定时任务
- function timer.clear()
- timer._timers = {}
- end
- function myFunction()
- SL:print("执行了延迟执行!")
- end
- function shortcutF2() -- 开始执行!
- -- checkenemy()
- timer2 = SL:Schedule(timer.update, 1)
- timer.schedule(3, myFunction)
- end
- GUI:addKeyboardEvent("KEY_F2", pressedCB, shortcutF2) -- 按下触发
- function shortcutF3() -- 结束执行!
- SL:print("结束定时器!")
- SL:UnSchedule(timer2)
- end
- GUI:addKeyboardEvent("KEY_F3", pressedCB, shortcutF3) -- 按下触发
复制代码
|
|