setTimeout
Signature
Section titled “Signature”setTimeout(callback, delay, uniqueKey)Description
Section titled “Description”Runs callback after the specified delay time.
Returns integer which can be utilized by the clearTimeout() function.
Intervals are only called once per frame.
Parameters
Section titled “Parameters”| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
callback | function | Yes | - | Function to be called after the delay. |
delay | number | No | 0 | Delay time in seconds. |
uniqueKey | any | No | nil | Unique identifier for the timer. |
Return Values
Section titled “Return Values”integerCancellationID for the timeout. Can be used to cancel the timeout withclearTimeout().
Examples
Section titled “Examples”setTimeout(function() ac.log("This message is printed after 2 seconds")end, 2)local cancellationID = setTimeout(function() ac.shutdownAssettoCorsa()end, 3)
function script.windowMain() if ui.button("Prevent shutdown") then local cancelled = clearTimeout(cancellationID) ac.debug("prevented?", cancelled) endendsetTimeout(function() ac.log("Garbage collection")end, 2, "gc")
-- This second timer with the same uniqueKey won't be added.setTimeout(function() ac.log("This will not run")end, 2, "gc")
-- This timer with a different uniqueKey runs normally.setTimeout(function() ac.log("Other timer running")end, 2, "otherTimer")