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)
Maybe this will work
function sleep(n)
n = math.ceil(n)
if n <= 0 and n > 99999 then return end --If the user enter a number below 0 and higher than 99999 the limit in TIMEOUT command in Windows
os.execute("timeout /T "..tostring(seconds).." /NOBREAK")
end
This will not wasting CPU time by busy loop but "Waiting for n seconds, press CTRL+C to quit ..." In Windows you can use -1 but it will wait forever so i restrict it because waiting forever is useless. And if you don't want "Waiting for n seconds, press CTRL+C to quit ..." to appear i don't know for that can do same thing without display that. If you want get rid of that just add os.execute("cls") after the os.execute("timeout /T "..tostring(seconds).." /NOBREAK") statement and it will clear up the console
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.