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.
| Parameter | Type | Description |
|---|---|---|
msec | integer or function | Delay in ms, or a blocking "waiter" function |
cb | function | Callback to execute after the delay |
... | any | Arguments passed to cb |
Returns: TimeoutClass (cancellable handle)
tw.timeout.loop(msec, cb, ...)
Creates a recurring loop that executes the callback at a fixed interval.
| Parameter | Type | Description |
|---|---|---|
msec | integer or function | Interval in ms, or a blocking "waiter" function |
cb | function | Callback to execute each iteration |
... | any | Arguments 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.
| Parameter | Type | Description |
|---|---|---|
id | string | Unique identifier for the debounce group |
msec | integer | Delay in ms |
cb | function | Callback to execute |
... | any | Arguments 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.
| Parameter | Type | Description |
|---|---|---|
id | string | Unique identifier for the anti-spam group |
msec | integer | Cooldown duration in ms |
cb | function | Callback to execute |
... | any | Arguments passed to cb |
TimeoutClass Methods
The object returned by tw.timeout.set and tw.timeout.loop.
| Method | Description |
|---|---|
: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.setandtw.timeout.loopreturn aTimeoutClassobject. Keep a reference if you need to cancel later.tw.timeout.delayis ideal for search inputs or save-on-change patterns where you want to wait for the user to stop typing.tw.timeout.noSpamis ideal for interaction buttons where you want to prevent rapid repeated activation.- When
msecis a function instead of an integer, it acts as a custom blocking waiter (the function must yield/wait itself).

