setTimer | Multi Theft Auto: Wiki Skip to content

setTimer

Client-side
Server-side
Shared

Manual Review Required

Please finish this page using the corresponding Old Wiki article. Go to Contribution guidelines for more information.


This function allows you to trigger a function after a number of milliseconds have elapsed. You can call one of your own functions or a built-in function. For example, you could set a timer to spawn a player after a number of seconds have elapsed.

Note

The hidden global variable sourceTimer contains the currently executing timer userdata

Note

The hidden global variable source becomes nil inside a timer function. You need to declare it on the function arguments if you need to use it.

Important

The speed at which a client side timer runs can be completely unreliable if a client is maliciously modifying their operating system speed, timers could run much faster or slower.

Important

Writing the following code can cause performance issues. Use onClientPreRender instead. setTimer(theFunction, 0, 0)

OOP Syntax Help! I don't understand this!

  • Constructor: Timer(...)

Syntax

timer setTimer ( function theFunction, int timeInterval, int timesToExecute, var arguments... )
Required Arguments
  • theFunction: The function you wish the timer to call.
  • timeInterval: MISSING_PARAM_DESC
  • timesToExecute: MISSING_PARAM_DESC
  • arguments...: MISSING_PARAM_DESC

Returns

  • timer: value

Returns a timer pointer if the timer was set successfully, false if the arguments are invalid or the timer could not be set.

Code Examples

shared

This example will output some text after a small delay.

-- define function to be called
function delayedChat ( text )
outputChatBox ( "Delayed text: " .. text )
end
-- set a timer so the function is called after 1 second
setTimer ( delayedChat, 1000, 1, "Hello, World!" )