如何自动检查时间?

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

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小时不间断地运行程序...

感谢阅读。

原文链接 https://stackoverflow.com/questions/70304870

点赞
stackoverflow用户17637570
stackoverflow用户17637570

这可能不是最好的解决方案,但是使用像love2d这样的库时,你可以运行类似以下代码:


function love.update(dt)
 timer = os.date('%H:%M:%S', os.time() - 13 * 60 * 60 )
 if timer >= value then
  --执行操作
 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
   --执行操作
  end
 end
end
2021-12-10 13:30:21
stackoverflow用户11740758
stackoverflow用户11740758

Lua 必须以某种方式检查时间。

如果没有可以使用 debug.sethook() 实现的循环。

例如,在交互式 Lua(lua -i)中键入 Lua 5.1...

> print(_VERSION)
Lua 5.1
> debug.sethook() -- 这将清除已定义的钩子
> -- 接下来设置一个在 'line' 事件上触发的钩子函数
> 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')
-- 只需按回车键或执行其他操作
23:59:59

5.9 - 调试库

https://www.lua.org/manual/5.1/manual.html#5.9

2021-12-11 09:14:40