# Timeout (/docs/es_extended/shared/timeout)

`ESX.SetTimeout` runs a function once after a delay, like FiveM's `SetTimeout`, but returns an id so you can cancel it with `ESX.ClearTimeout`. Both work on client and server.

## Overview [#overview]

| | |
|---|---|
| Resource | `es_extended` (implemented in `esx_lib` as `xLib.timeout`) |
| Side | Shared |

## Functions [#functions]

### ESX.SetTimeout [#esxsettimeout]

```lua
ESX.SetTimeout(msec, cb)
```

| Name | Type | Description |
|---|---|---|
| `msec` | `number` | Delay in milliseconds. |
| `cb` | `function` | Function to run. Raises an error if it is not callable. |

**Returns:** `number`, the timeout id.

```lua title="server/main.lua"
local handcuffTimers = {}

RegisterNetEvent('police:cuffed', function(target)
    local xPlayer = ESX.GetPlayerFromId(source)
    if not xPlayer or xPlayer.job.name ~= 'police' then return end

    handcuffTimers[target] = ESX.SetTimeout(10 * 60 * 1000, function()
        handcuffTimers[target] = nil
        TriggerClientEvent('police:forceUncuff', target)
    end)
end)
```

### ESX.ClearTimeout [#esxcleartimeout]

```lua
ESX.ClearTimeout(id)
```

| Name | Type | Description |
|---|---|---|
| `id` | `number` | Id returned by `ESX.SetTimeout`. |

**Returns:** nothing

```lua
RegisterNetEvent('police:uncuffed', function(target)
    if handcuffTimers[target] then
        ESX.ClearTimeout(handcuffTimers[target])
        handcuffTimers[target] = nil
    end
end)
```

## Common issues [#common-issues]

- **Timers live in es_extended.** `ESX.SetTimeout` runs inside `es_extended`, so restarting `es_extended` drops all pending timeouts. If your own resource restarts first, its callbacks can no longer run.
- **Clearing twice is harmless**, but clearing an id that already ran leaves a small entry behind. Set your stored id to `nil` after the callback runs, as in the example.

## Related [#related]

- [Shared functions (ESX.Await)](/docs/es_extended/shared)
- [Table functions](/docs/es_extended/shared/table)
- [Performance guide](/docs/guides/performance)
