lubyk logo

Lubyk documentation

lens.Timer

The Timer contains a callback to execute a function at regular intervals. This timer does not drift (uses OS monotonic clock).

Usage:

local i = 0
local tim = lens.Timer(0.5, function()
  print('Hello', lens.elapsed())
  i = i + 1
  if i == 10 then
    -- Use return value to set new interval. A
    -- return value of 0 stops the timer.
    return 0
  end
end)

Constructor

.new (interval, callback)

Create a new timer with a give interval in seconds and a callback function. The callback can also be set as timeout method on the returned object.

Start, stop, phase

:start (start_in_seconds)

Start timer. The start_in_seconds parameter is the delay to start the timer. For precise phase synchronization with other timers, use startAt. For irregular timers, you should use timeout return value instead.

:startAt (at)

Start timer with a precise starting time. This can be used to set precision phase between timers.

:stop ()

Stop the timer.

Methods

:running ()

Return true if the timmer is running.

:setInterval (interval)

Change interval but do not restart timer (effect on next trigger).

Callback

:timeout ()

Method called when the timer fires. If you return a number from this method, it will be used as the next interval but it does not alter the timer interval.

Returning a number can be used for irregular timers or to change phase. A returned value of 0 stops the timer.