Back to community

tip: stop using SetTimeout, use Wait inside CreateThread

55 rep217 views1 min read

was helping a friend debug his server today and i noticed he was doing this everywhere:

SetTimeout(60000, function()
    -- do something every minute
    SetTimeout(60000, function()
        -- repeat
        ...
    end)
end)

PLEASE just do:

CreateThread(function()
    while true do
        Wait(60000)
        -- do something every minute
    end
end)

cleaner, easier to read, no recursion. SetTimeout is for one-shot delayed actions, not recurring tasks.

basic but i see this constantly.

19 2 commentsSign in to vote

Comments (2)

Sign in to leave a comment.
  • guilty. switching tonight, thanks for the obvious tip i somehow missed

    1 points
  • and if you want a cleaner abstraction: SetInterval(function() ... end, 60000) is basically syntactic sugar for the CreateThread version

    1 points