autodoc_shared_functions
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 <br>
JO Functions > jo.timeout.delay() > Syntax
jo.timeout.delay(id, msec, cb, ...)
JO Functions > jo.timeout.delay() > 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
... : any
Additional arguments to pass to the callback function
JO Functions > jo.timeout.delay() > Return Value
Type : TimeoutClass
Return the timeout instance
JO Functions > jo.timeout.delay() > Example
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 Functions > jo.timeout.loop()
Create a loop to execute the function at regular interval <br>
JO Functions > jo.timeout.loop() > Syntax
jo.timeout.loop(msec, cb, ...)
JO Functions > jo.timeout.loop() > Parameters
msec : integer
The duration between two executions of cb
cb : function
The function executed every msec ms
... : any
Additional arguments to pass to the callback function
JO Functions > jo.timeout.loop() > Return Value
Type : TimeoutClass
Return the timeout instance
JO Functions > jo.timeout.loop() > Example
jo.timeout.loop(1000, function()
print(GetGameTimer())
end)
-- Expected output: The GetGameTimer() will be printed every 1000 msec
JO Functions > jo.timeout.noSpam()
A function to delay the second execution. If another delay is created with the same id, the previous one is canceled <br>
JO Functions > jo.timeout.noSpam() > Syntax
jo.timeout.noSpam(id, msec, cb, ...)
JO Functions > jo.timeout.noSpam() > 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
... : any
Additional arguments to pass to the callback function
JO Functions > jo.timeout.noSpam() > Return Value
Type : TimeoutClass
Return the timeout instance
JO Functions > jo.timeout.noSpam() > Example
local id = "TheUniqueID"
local noSpam = jo.timeout.noSpam(id, 1000, function() print('Done') end)
Wait(500)
local noSpam2 = jo.timeout.noSpam(id, 1000, function() print('Done 2') end)
local noSpam3 = jo.timeout.noSpam(id, 1000, function() print('Done 3') end)
-- Expected output : "Done" printed after 1000ms and "Done 3" printed after 1500ms (500 + 1000). noSpam2 is canceled by noSpam3 because it's the same id
JO Functions > jo.timeout.set() > Syntax
jo.timeout.set(msec, cb, ...)
JO Functions > jo.timeout.set() > 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
... : any
Additional arguments to pass to the callback function
JO Functions > jo.timeout.set() > Return Value
Type : TimeoutClass
Return the timeout instance
JO Functions > jo.timeout.set() > Example
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 <br> Prevents the callback from being executed <br>
TimeoutClass Methods > TimeoutClass:clear() > Syntax
TimeoutClass:clear()
TimeoutClass Methods > TimeoutClass:clear() > Example
local timeout = jo.timeout.set(1000, function() print("Done") end)
timeout:clear()
-- Expected output : Nothing because the function was canceled
TimeoutClass Methods > TimeoutClass:execute()
Execute the callback function <br> Automatically clears the timeout and passes any stored arguments to the callback <br>
TimeoutClass Methods > TimeoutClass:execute() > Syntax
TimeoutClass:execute()
TimeoutClass Methods > TimeoutClass:execute() > Example
local timeout = TimeoutClass:set(1000, function() print('Done') end)
timeout:execute()
-- Expected output: 'Done' after 1000 ms
TimeoutClass Methods > TimeoutClass:set() > Syntax
TimeoutClass:set(msec, cb, args)
TimeoutClass Methods > TimeoutClass:set() > Parameters
msec : integer|function
The waiter of the function
cb : function
The function to execute after waiter
args : table <BadgeOptional />
Optional arguments to pass to the callback function
TimeoutClass Methods > TimeoutClass:set() > Return Value
Type : TimeoutClass
Return the timeout instance
TimeoutClass Methods > TimeoutClass:set() > Example
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 Methods > TimeoutClass:setCb()
Change the callback function of the timeout <br>
TimeoutClass Methods > TimeoutClass:setCb() > Syntax
TimeoutClass:setCb(cb)
TimeoutClass Methods > TimeoutClass:setCb() > Parameters
cb : function
The new callback function
TimeoutClass Methods > TimeoutClass:setCb() > Example
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 Methods > TimeoutClass:setMsec()
Change the timeout duration or waiter function <br>
TimeoutClass Methods > TimeoutClass:setMsec() > Syntax
TimeoutClass:setMsec(msec)
TimeoutClass Methods > TimeoutClass:setMsec() > Parameters
msec : integer|function
New timeout duration in ms or a new waiter function
TimeoutClass Methods > TimeoutClass:setMsec() > Example
local timeout = jo.timeout.set(1000, function(), print('Done') end)
timeout:setMsec(2000)
-- Executed output: 'Done' printed after 2000ms
TimeoutClass Methods > TimeoutClass:start()
Start the timeout by initiating the waiting period <br> Either waits for msec milliseconds or executes the waiter function <br>
TimeoutClass Methods > TimeoutClass:start() > Syntax
TimeoutClass:start()
TimeoutClass Methods > TimeoutClass:start() > Example
-- 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