• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community

wait() Function (1 Viewer)

The wait() function allows the program to wait until the applied amount of seconds ended.

function:
Lua:
function wait(sec, debug)
    t = os.clock()  --store time when the wait function started
    tp = t + sec    --store the end time of the wait function
    while true do   --loop that goes forever until current time is equal or greater than the end time
        if os.clock() >= tp then
            if debug == "*d" then  --(optional) debug mode
                print("ended at "..os.clock().." From "..t.." (in os time: min "..os.date("%M").." sec "..os.date("%S")..'") Time Elapsed: '..(os.clock()-t))
            end
            break   --break the loop if the statement is true
        end
    end
end

example code: timer
Lua:
s,m,h = 0,0,0  --second, minute, hour

while true do
    print(s,":",m,":",h)
    s = s+1
    if s >= 60 then
        s = 0
        m = m + 1
    end
    if m >= 60 then
        m = 0
        s = 0
        h = h + 1
    end
    wait(1)
end

debug mode:
Lua:
--Normal
print("hello")
wait(1)
print("world")

--Debug
print("hello")
wait(1, "*d")
print("world")
 
Last edited:
Top