Lua Program Delay

Viewed 33527

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
2 Answers

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

Related