OPX//77
opx_lib

Lib.Timer

Last updated on

Delays, debounce, throttle and wait-until without writing a thread each time.

No permission. Everything it returns is a closure and lives in your VM.

Lib.Timer.After

Runs a function once after a delay.

Lib.Timer.After(delay, run)

Parameters

  • delay: integer — milliseconds
  • run: function

Returns

  • any? — a handle for Cancel, nil if the arguments were wrong
Example
local hint = Lib.Timer.After(3000, function() Lib.Notify.Show('Press E') end)

Lib.Timer.Cancel

Cancels a timer from After.

Lib.Timer.Cancel(handle)

Parameters

  • handle: any

Returns

  • boolean

Lib.Timer.Debounce

Wraps a function so it runs once, wait ms after the last call, with the last call's arguments.

Lib.Timer.Debounce(wait, run)

Parameters

  • wait: integer — milliseconds
  • run: function

Returns

  • function
Example
local save = Lib.Timer.Debounce(500, function(value) Lib.Store.Set('volume', value) end)

Lib.Timer.Throttle

Wraps a function: the first call runs at once, then at most one more per window, with the latest arguments.

Lib.Timer.Throttle(window, run)

Parameters

  • window: integer — milliseconds
  • run: function

Returns

  • function

Lib.Timer.Until

Waits until test answers truthy, or the deadline passes. Yields.

Lib.Timer.Until(test, timeout, interval)

Parameters

  • test: function
  • timeout?: integer
    • Milliseconds. Default: 5000.
  • interval?: integer
    • Milliseconds between tries. Default: 50.

Returns

  • any? — the first truthy answer, or nil on timeout
Example
CreateThread(function()
  local ready = Lib.Timer.Until(function() return Lib.Character.Position().ok end, 10000)
  if not ready then return print('no body after 10 s') end
end)

On this page