找回密码
 立即注册
    查看: 5|回复: 0

    前端延时call

    [复制链接]

    326

    主题

    66

    回帖

    1466

    积分

    积分
    1466
    发表于 2025-2-24 02:01:31 | 显示全部楼层 |阅读模式

    1. -- 定时器表,存储所有定时任务
    2. timer = {}
    3. timer._timers = {}

    4. function timer.schedule(delay, callback)  -- 加入定时器和函数到表
    5.     table.insert(timer._timers, {time = os.clock() + delay, callback = callback})
    6. end

    7. -- 更新定时器,检查是否有定时任务到期
    8. function timer.update()
    9.     new_dd = (new_dd or 0) + 1
    10.     SL:print("执行定时器"..new_dd)
    11.     local now = os.clock()
    12.     for i = #timer._timers, 1, -1 do
    13.         local t = timer._timers[i]
    14.         SL:print("检测定时器时间:",now , t.time )
    15.         if now >= t.time then
    16.             SL:print("到时间了!")
    17.             t.callback()
    18.             table.remove(timer._timers, i)  -- 移除已完成的定时任务<!--EndFragment-->

    19.         end
    20.     end
    21. end

    22. -- 清除所有定时任务
    23. function timer.clear()
    24.     timer._timers = {}
    25. end

    26. function myFunction()
    27.     SL:print("执行了延迟执行!")
    28. end
    29. function shortcutF2()   -- 开始执行!
    30.     -- checkenemy()
    31.     timer2 =  SL:Schedule(timer.update, 1)
    32.     timer.schedule(3, myFunction)
    33. end

    34. GUI:addKeyboardEvent("KEY_F2", pressedCB, shortcutF2)  -- 按下触发

    35. function shortcutF3()   -- 结束执行!
    36.     SL:print("结束定时器!")
    37.     SL:UnSchedule(timer2)
    38. end


    39. GUI:addKeyboardEvent("KEY_F3", pressedCB, shortcutF3)  -- 按下触发
    复制代码



    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则