Lua程序延迟 [英] Lua Program Delay

查看:1106
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆