Lua Program Delay

后端 未结 2 703
萌比男神i
萌比男神i 2021-01-13 02:15

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)
          


        
相关标签:
2条回答
  • 2021-01-13 03:04

    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

    0 讨论(0)
  • 2021-01-13 03:07

    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.

    0 讨论(0)
提交回复
热议问题