Skip to content

Timeout Shared

The Timeout module provides a set of utilities to manage script execution timing and thread control. It allows you to create delays, loops, and timeouts with easy-to-use functions that abstract away the complexity of thread management.

JO Functions

jo.timeout.delay()

A function to delay execution. If another delay is created with the same id, the previous one will be canceled

Syntax

lua
jo.timeout.delay(id, msec, cb, ...)

Parameters

id : string

The unique ID of the delay

msec : integer|function

The duration before execute cb or a waiter function

cb : function

The function executed after msec

... : mixed

Additional arguments to pass to the callback function

Return Value

Type : TimeoutClass

Return the timeout instance

Example

lua
local id = "TheUniqueID"
local delay = jo.timeout.delay(id, 1000, function() print('Done') end)
Wait(500)
local delay2 = jo.timeout.delay(id, 1000, function() print('Done 2') end)
-- Expected output : "Done 2" printed after 1500ms (500 + 1000). delay function was canceled by delay2 because it's the same id

jo.timeout.loop()

Create a loop to execute the function at regular interval

Syntax

lua
jo.timeout.loop(msec, cb, ...)

Parameters

msec : integer

The duration between two executions of cb

cb : function

The function executed every msec ms

... : mixed

Additional arguments to pass to the callback function

Return Value

Type : TimeoutClass

Return the timeout instance

Example

lua
jo.timeout.loop(1000, function()
    print(GetGameTimer())
end)
-- Expected output: The GetGameTimer() will be printed every 1000 msec

jo.timeout.set()

A function to set a timeout

Syntax

lua
jo.timeout.set(msec, cb, ...)

Parameters

msec : integer|function

If integer: wait duration in ms. If function: the function will be executed before cb

cb : function

The function executed when waiter is done

... : mixed

Additional arguments to pass to the callback function

Return Value

Type : TimeoutClass

Return the timeout instance

Example

lua
local waiter = 1000
local cb = function()
    print('Fired')
end
jo.timeout.set(waiter, cb)
-- Expected output: 'Fired' after 1000 ms

TimeoutClass Methods

TimeoutClass:clear()

Cancel the timeout
Prevents the callback from being executed

Syntax

lua
TimeoutClass:clear()

Example

lua
local timeout = jo.timeout.set(1000, function() print("Done") end)
timeout:clear()
-- Expected output : Nothing because the function was canceled

TimeoutClass:execute()

Execute the callback function
Automatically clears the timeout and passes any stored arguments to the callback

Syntax

lua
TimeoutClass:execute()

Example

lua
local timeout = TimeoutClass:set(1000, function() print('Done') end)
timeout:execute()
-- Expected output: 'Done' after 1000 ms

TimeoutClass:set()

Initialize a new timeout

Syntax

lua
TimeoutClass:set(msec, cb, args)

Parameters

msec : integer|function

The waiter of the function

cb : function

The function to execute after waiter

args : table Optional

Optional arguments to pass to the callback function

Return Value

Type : TimeoutClass

Return the timeout instance

Example

lua
local msec = 1000
local function callback()
  print("Timeout completed")
end
local args = { name = "Test", value = 100 }

-- Initialize a new timeout without starting it
local timeout = TimeoutClass:set(msec, callback, args)
print("Timeout created with ID: " .. timeout.id)

-- The timeout has been created but won't execute until start() is called
-- Expected output: Just prints the timeout ID, callback won't run automatically

TimeoutClass:setCb()

Change the callback function of the timeout

Syntax

lua
TimeoutClass:setCb(cb)

Parameters

cb : function

The new callback function

Example

lua
local timeout = jo.timeout.set(1000, function() print('Done') end)
timeout:setCb(function()
    print('Done overwrited')
end)
-- Expected output: "Done overwrited" printed after 1000ms

TimeoutClass:setMsec()

Change the timeout duration or waiter function

Syntax

lua
TimeoutClass:setMsec(msec)

Parameters

msec : integer|function

New timeout duration in ms or a new waiter function

Example

lua
local timeout = jo.timeout.set(1000, function(), print('Done') end)
timeout:setMsec(2000)
--Executed output: 'Done' printed after 2000ms

TimeoutClass:start()

Start the timeout by initiating the waiting period
Either waits for msec milliseconds or executes the waiter function

Syntax

lua
TimeoutClass:start()

Example

lua
-- Create a timeout without starting it
local timeout = TimeoutClass:set(1500, function()
  print("Timeout triggered after manual start")
end)

-- Later in the code, decide when to start the timeout
print("Starting the timeout now...")
timeout:start()

-- Expected output:
-- "Starting the timeout now..." immediately
-- "Timeout triggered after manual start" after 1500ms

Last updated: