Timeout
ESX.SetTimeout and ESX.ClearTimeout run code after a delay in ESX Legacy and let you cancel it, on both client and server. Includes examples and pitfalls.
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
| Resource | es_extended (implemented in esx_lib as xLib.timeout) |
| Side | Shared |
Functions
ESX.SetTimeout
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.
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
ESX.ClearTimeout(id)| Name | Type | Description |
|---|---|---|
id | number | Id returned by ESX.SetTimeout. |
Returns: nothing
RegisterNetEvent('police:uncuffed', function(target)
if handcuffTimers[target] then
ESX.ClearTimeout(handcuffTimers[target])
handcuffTimers[target] = nil
end
end)Common issues
- Timers live in es_extended.
ESX.SetTimeoutruns insidees_extended, so restartinges_extendeddrops 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
nilafter the callback runs, as in the example.
Related
Frequently asked questions
Independent documentation
Last updated on
Table
ESX.Table reference for ESX Legacy - SizeOf, Find, Filter, Map, Clone, Concat, Join, TableContains, Sort, ToArray and more, with return values and pitfalls.
Client PlayerData
What ESX.PlayerData contains on the client in ESX Legacy, how it stays in sync with the server, and how to react to changes with OnPlayerData and esx:setPlayerData.