Skip to content

Class Timer ​

A callback scheduled with Timer.after or Timer.every. Keep the handle to cancel it.

  • A timer can run up to about 20 ms late.
  • The callback gets no arguments and has its own instruction budget. An error in it is logged.
  • A reload drops every timer of the old config. The new config starts its own.

Functions ​

Timer.after(delay_ms: number, callback: function) -> Timerafter ​

Runs callback once, delay_ms milliseconds from now.

  • A delay of 0 runs it after the current event is done.
  • A negative delay counts as 0.
lua
wm.outputs:on("removed", function()
	Timer.after(0, function()
		print("outputs left:", wm.outputs:count())
	end)
end)

Timer.every(interval_ms: number, callback: function) -> Timerevery ​

Runs callback every interval_ms milliseconds until the timer is cancelled.

  • The first run is interval_ms from now.
  • Each next run is counted from when the last one ran. A late run pushes back the ones after it; they don't bunch up to catch up.
  • An interval below 1 counts as 1.
  • An error in the callback is logged and the timer keeps going.
lua
local seconds = 0
local ticker = Timer.every(1000, function()
	seconds = seconds + 1
end)
Timer.after(5000, function() ticker:cancel() end)

Timer.now() -> integernow ​

Milliseconds since Hazel started, on the clock timers use. It keeps counting across reloads.

lua
local last_click_ms
local function is_double_click()
	local now = Timer.now()
	local double = last_click_ms ~= nil and now - last_click_ms < 400
	last_click_ms = not double and now or nil
	return double
end

Methods ​

timer:cancel() -> booleancancel ​

Stops the timer. A repeating timer can cancel itself from its own callback.

  • true when it was still scheduled.
  • false when a one-shot timer has already run, or the timer was already cancelled.