From the reference libraryRedM

Timeout <BadgeShared/>

jo_libs/modules/timeout/shared.md

Timeout <BadgeShared/>

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()

<!-- @include: ./slots/headers.md#shared|jo.timeout.delay -->

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

<!-- @include: ./slots/descriptions.md#shared|jo.timeout.delay -->

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

<!-- @include: ./slots/examples.md#shared|jo.timeout.delay -->

<!-- @include: ./slots/footers.md#shared|jo.timeout.delay -->


JO Functions > jo.timeout.loop()

<!-- @include: ./slots/headers.md#shared|jo.timeout.loop -->

Create a loop to execute the function at regular interval <br>

<!-- @include: ./slots/descriptions.md#shared|jo.timeout.loop -->

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

<!-- @include: ./slots/examples.md#shared|jo.timeout.loop -->

<!-- @include: ./slots/footers.md#shared|jo.timeout.loop -->


JO Functions > jo.timeout.noSpam()

<!-- @include: ./slots/headers.md#shared|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>

<!-- @include: ./slots/descriptions.md#shared|jo.timeout.noSpam -->

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

<!-- @include: ./slots/examples.md#shared|jo.timeout.noSpam -->

<!-- @include: ./slots/footers.md#shared|jo.timeout.noSpam -->


JO Functions > jo.timeout.set()

<!-- @include: ./slots/headers.md#shared|jo.timeout.set -->

A function to set a timeout <br>

<!-- @include: ./slots/descriptions.md#shared|jo.timeout.set -->

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

<!-- @include: ./slots/examples.md#shared|jo.timeout.set -->

<!-- @include: ./slots/footers.md#shared|jo.timeout.set -->

TimeoutClass Methods > TimeoutClass:clear()

<!-- @include: ./slots/headers.md#shared|TimeoutClass:clear -->

Cancel the timeout <br> Prevents the callback from being executed <br>

<!-- @include: ./slots/descriptions.md#shared|TimeoutClass:clear -->

TimeoutClass Methods > TimeoutClass:clear() > Syntax

TimeoutClass:clear()

<!-- @include: ./slots/examples.md#shared|TimeoutClass:clear -->

<!-- @include: ./slots/footers.md#shared|TimeoutClass:clear -->


TimeoutClass Methods > TimeoutClass:execute()

<!-- @include: ./slots/headers.md#shared|TimeoutClass:execute -->

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

<!-- @include: ./slots/descriptions.md#shared|TimeoutClass:execute -->

TimeoutClass Methods > TimeoutClass:execute() > Syntax

TimeoutClass:execute()

<!-- @include: ./slots/examples.md#shared|TimeoutClass:execute -->

<!-- @include: ./slots/footers.md#shared|TimeoutClass:execute -->


TimeoutClass Methods > TimeoutClass:set()

<!-- @include: ./slots/headers.md#shared|TimeoutClass:set -->

Initialize a new timeout <br>

<!-- @include: ./slots/descriptions.md#shared|TimeoutClass:set -->

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

<!-- @include: ./slots/examples.md#shared|TimeoutClass:set -->

<!-- @include: ./slots/footers.md#shared|TimeoutClass:set -->


TimeoutClass Methods > TimeoutClass:setCb()

<!-- @include: ./slots/headers.md#shared|TimeoutClass:setCb -->

Change the callback function of the timeout <br>

<!-- @include: ./slots/descriptions.md#shared|TimeoutClass:setCb -->

TimeoutClass Methods > TimeoutClass:setCb() > Syntax

TimeoutClass:setCb(cb)

TimeoutClass Methods > TimeoutClass:setCb() > Parameters

cb : function

The new callback function

<!-- @include: ./slots/examples.md#shared|TimeoutClass:setCb -->

<!-- @include: ./slots/footers.md#shared|TimeoutClass:setCb -->


TimeoutClass Methods > TimeoutClass:setMsec()

<!-- @include: ./slots/headers.md#shared|TimeoutClass:setMsec -->

Change the timeout duration or waiter function <br>

<!-- @include: ./slots/descriptions.md#shared|TimeoutClass:setMsec -->

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

<!-- @include: ./slots/examples.md#shared|TimeoutClass:setMsec -->

<!-- @include: ./slots/footers.md#shared|TimeoutClass:setMsec -->


TimeoutClass Methods > TimeoutClass:start()

<!-- @include: ./slots/headers.md#shared|TimeoutClass:start -->

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

<!-- @include: ./slots/descriptions.md#shared|TimeoutClass:start -->

TimeoutClass Methods > TimeoutClass:start() > Syntax

TimeoutClass:start()

<!-- @include: ./slots/examples.md#shared|TimeoutClass:start -->

<!-- @include: ./slots/footers.md#shared|TimeoutClass:start -->

Back to documentation