# 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 <br>

#### 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
>

`...` : _any_
> Additional arguments to pass to the callback function
>

#### Return Value

Type : _[TimeoutClass](#timeoutclass-methods)_

> Return the timeout instance

---

### jo.timeout.loop()

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

#### 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
>

`...` : _any_
> Additional arguments to pass to the callback function
>

#### Return Value

Type : _[TimeoutClass](#timeoutclass-methods)_

> Return the timeout instance

---

### 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>

#### Syntax

```lua
jo.timeout.noSpam(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
>

`...` : _any_
> Additional arguments to pass to the callback function
>

#### Return Value

Type : _[TimeoutClass](#timeoutclass-methods)_

> Return the timeout instance

---

### jo.timeout.set()

A function to set a timeout <br>

#### 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
>

`...` : _any_
> Additional arguments to pass to the callback function
>

#### Return Value

Type : _[TimeoutClass](#timeoutclass-methods)_

> Return the timeout instance

## TimeoutClass Methods

### TimeoutClass:clear()

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

#### Syntax

```lua
TimeoutClass:clear()
```

---

### TimeoutClass:execute()

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

#### Syntax

```lua
TimeoutClass:execute()
```

---

### TimeoutClass:set()

Initialize a new timeout <br>

#### 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](#timeoutclass-methods)_

> Return the timeout instance

---

### TimeoutClass:setCb()

Change the callback function of the timeout <br>

#### Syntax

```lua
TimeoutClass:setCb(cb)
```

#### Parameters

`cb` : _function_
> The new callback function
>

---

### TimeoutClass:setMsec()

Change the timeout duration or waiter function <br>

#### Syntax

```lua
TimeoutClass:setMsec(msec)
```

#### Parameters

`msec` : _integer|function_
> New timeout duration in ms or a new waiter function
>

---

### TimeoutClass:start()

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

#### Syntax

```lua
TimeoutClass:start()
```
