Skip to content

tw.timeout

Side: Shared (client + server)

Async utilities: timeouts, recurring loops, debounce, and anti-spam.

Functions

tw.timeout.set(msec, cb, ...)

Executes a callback after a delay.

ParameterTypeDescription
msecinteger or functionDelay in ms, or a blocking "waiter" function
cbfunctionCallback to execute after the delay
...anyArguments passed to cb

Returns: TimeoutClass (cancellable handle)


tw.timeout.loop(msec, cb, ...)

Creates a recurring loop that executes the callback at a fixed interval.

ParameterTypeDescription
msecinteger or functionInterval in ms, or a blocking "waiter" function
cbfunctionCallback to execute each iteration
...anyArguments passed to cb

Returns: TimeoutClass — call :clear() to stop the loop.


tw.timeout.delay(id, msec, cb, ...)

Debounce: cancels any previous timeout with the same id and restarts the timer. Only the last call within the delay window will execute.

ParameterTypeDescription
idstringUnique identifier for the debounce group
msecintegerDelay in ms
cbfunctionCallback to execute
...anyArguments passed to cb

tw.timeout.noSpam(id, msec, cb, ...)

Anti-spam: the first call executes immediately, then subsequent calls within the delay window are ignored.

ParameterTypeDescription
idstringUnique identifier for the anti-spam group
msecintegerCooldown duration in ms
cbfunctionCallback to execute
...anyArguments passed to cb

TimeoutClass Methods

The object returned by tw.timeout.set and tw.timeout.loop.

MethodDescription
:clear()Cancel the timeout or stop the loop
:start()Start manually (called automatically on creation)
:execute()Execute the callback immediately
:setCb(cb)Change the callback function
:setMsec(msec)Change the delay/interval

Examples

Simple delayed action

lua
tw.timeout.set(5000, function()
    print("5 seconds passed")
end)

Cancellable timeout

lua
local timer = tw.timeout.set(10000, function()
    print("This may never print")
end)

-- Cancel before it fires
timer:clear()

Recurring loop

lua
local loop = tw.timeout.loop(1000, function()
    print("Every second")
end)

-- Stop the loop later
loop:clear()

Debounce (only last call executes)

lua
-- If called multiple times rapidly, only the last call runs (2s after the last trigger)
tw.timeout.delay("save_position", 2000, function()
    savePlayerPosition()
end)

Anti-spam (first call only, cooldown blocks the rest)

lua
-- Runs immediately on first call, then ignores calls for 3 seconds
tw.timeout.noSpam("collect", 3000, function()
    startCollection()
end)

Passing arguments

lua
tw.timeout.set(2000, function(item, amount)
    print("Collected " .. amount .. "x " .. item)
end, "apple", 5)

Notes

  • tw.timeout.set and tw.timeout.loop return a TimeoutClass object. Keep a reference if you need to cancel later.
  • tw.timeout.delay is ideal for search inputs or save-on-change patterns where you want to wait for the user to stop typing.
  • tw.timeout.noSpam is ideal for interaction buttons where you want to prevent rapid repeated activation.
  • When msec is a function instead of an integer, it acts as a custom blocking waiter (the function must yield/wait itself).

Premium RedM Scripts — Multi-Framework