FiveM Docs
es_extendedShared

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

Resourcees_extended (implemented in esx_lib as xLib.timeout)
SideShared

Functions

ESX.SetTimeout

ESX.SetTimeout(msec, cb)
NameTypeDescription
msecnumberDelay in milliseconds.
cbfunctionFunction to run. Raises an error if it is not callable.

Returns: number, the timeout id.

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

ESX.ClearTimeout(id)
NameTypeDescription
idnumberId 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.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.

Frequently asked questions

Independent documentation

esx-fivem.com is a community project and is not affiliated with the ESX Framework team. Facts on this page were checked against the upstream source. Official resources: docs.esx-framework.org and github.com/esx-framework.
Edit on GitHub

Last updated on

On this page