【问题标题】:How to automatically check time?如何自动检查时间?
【发布时间】:2022-01-15 04:00:45
【问题描述】:

是否可以自动检查时间然后执行某些代码?

timer = os.date('%H:%M:%S', os.time() - 13 * 60 * 60 )

if timer == "18:04:40" then
   print("hello")
end

我正在尝试在每天“18:04:40”(os.date 的时间)打印hello,而没有设置计时器(它计算自程序启动以来过去的时间),因为我无法运行节目24小时不间断...

感谢阅读。

【问题讨论】:

    标签: time lua


    【解决方案1】:

    这可能不是最好的解决方案,但是,例如,当使用 love2d 之类的库时,您可以运行如下内容:

    
    function love.update(dt)
     timer = os.date('%H:%M:%S', os.time() - 13 * 60 * 60 )
     if timer >= value then 
      --stuff here
     end
    end
    

    或者,如果你想这样做,所以你有一个像这样的整数

    tick = 0
    function love.update(dt)
     tick = tick + dt 
     if tick > 1 then 
      timer = os.date('%H:%M:%S', os.time() - 13 * 60 * 60 )
      if timer >= value then 
       --stuff here
      end
     end
    end
    

    【讨论】:

      【解决方案2】:

      Lua 必须以某种方式检查时间。
      没有可以用debug.sethook() 实现的循环。
      以 Lua 5.1 输入交互式 Lua (lua -i) 的示例...

      > print(_VERSION)
      Lua 5.1
      > debug.sethook() -- This clears a defined hook
      > -- Next set up a hook function that fires on 'line' events
      > debug.sethook(function() local hour, min, sec = 23, 59, 59 print(os.date('%H:%M:%S', os.time({year = 2021, month = 12, day = 11, hour = hour, min = min, sec = sec}))) end, 'l')
      -- just hit return/enter or do other things
      23:59:59
      

      5.9 - 调试库
      https://www.lua.org/manual/5.1/manual.html#5.9

      【讨论】:

        猜你喜欢
        • 2014-05-29
        • 2013-05-22
        • 1970-01-01
        • 1970-01-01
        • 2020-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-29
        相关资源
        最近更新 更多