Lua 程序延迟 [英] Lua Program Delay
本文介绍了Lua 程序延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将如何使用它为我的 Lua 程序添加 2 分钟的延迟,这里是延迟的代码,但我不知道如何添加延迟.
How would I use this to add a delay of 2 minutes to my Lua program, here is the code for the delay, but I dont know how to add the delay.
function sleep(n)
local t = os.clock()
while os.clock() - t <= n do
-- nothing
end
end
推荐答案
os.clock
函数返回程序的 CPU 时间的秒数.所以你的sleep
函数会等待n
秒,如果你需要延迟2分钟,直接调用:
The os.clock
function returns the number of seconds of CPU time for the program. So the sleep
function of yours waits for n
seconds, if you need to delay 2 minutes, just call:
sleep(2*60)
请注意,除了忙等待之外,还有一些更好的解决方案来实现sleep
功能,参见睡眠功能了解详情.
Note that there are some better solutions to implement sleep
functions other than busy waiting, see Sleep Function for detail.
这篇关于Lua 程序延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文