Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua Program Delay

Tags:

time

delay

lua

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
like image 426
Petzl11 Avatar asked Dec 11 '13 06:12

Petzl11


People also ask

How do you delay in Lua?

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.

Is there a wait 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.

How do you comment on Lua?

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 ---[[ .

How do I declare a variable in Lua?

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.


1 Answers

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.

like image 66
Yu Hao Avatar answered Sep 29 '22 07:09

Yu Hao