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
Whenever there is a need to pause the program that we are executing for a certain number of seconds without making use of busy or waiting, then we make use of a function called sleep() function in Lua. The sleep() function is not a built-in function in Lua.
The Lua wait is a function used when multiple processes simultaneously and want the primary process to stop for a given time. It is a method useful for stopping or delays the parent process until the child process is not working completely.
A comment starts with a double hyphen ( -- ) anywhere outside a string. They run until the end of the line. You can comment out a full block of code by surrounding it with --[[ and --]] . To uncomment the same block, simply add another hyphen to the first enclosure, as in ---[[ .
The syntax for declaring a global variable in Lua is pretty straightforward, just declare whatever name you want to use for your variable and assign a value to it. It should be noted that we cannot declare a global variable without assigning any value to it, as it is not something that Lua allows us to do.
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)
Note that there are some better solutions to implement sleep
functions other than busy waiting, see Sleep Function for detail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With