IMIsle Modkit
Pages in this area

timers

Shared

Timers run on the game thread between game calls. The server checks them about every 25 ms, the client about every 200 ms. See the timers guide for patterns.

modkit.setTimeout

modkit.setTimeout(ms, fn)

Runs fn once after ms milliseconds.

Parameter Type Description
ms integer delay in milliseconds. Negative values count as 0. A float such as 1.5 raises an error.
fn function called without arguments

Returns: timer id (integer).

modkit.setTimeout(3000, function() print("three seconds later") end)

modkit.setInterval

modkit.setInterval(ms, fn)

Runs fn every ms milliseconds until it is cleared. With ms = 0 it runs on every timer check.

Parameter Type Description
ms integer interval in milliseconds
fn function called without arguments

Returns: timer id (integer).

When the game stalls, missed runs are not made up. The next run is scheduled from the moment the last one started.

local seconds = 0
local id = modkit.setInterval(1000, function()
  seconds = seconds + 1
  print("running for " .. seconds .. " s")
end)

modkit.clearTimer

modkit.clearTimer(id)

Stops a timeout or an interval. Unknown ids are ignored. A timer can clear itself from inside its function.

Parameter Type Description
id integer id from setTimeout or setInterval

Returns: nothing.

local runs = 0
local id
id = modkit.setInterval(500, function()
  runs = runs + 1
  if runs == 10 then modkit.clearTimer(id) end
end)