Petr Medek edited this page Feb 5, 2019 · 4 revisions

Basic syntax

From version 0.1.1.88 luamacros support timers. Timer can be set with built in function

lmc_set_timer(time_ms, handler)

Function accepts 2 parameters

  • time in milliseconds. After this time the handler function is executed
  • handler. This is lua function that is executed after specified number of seconds

The handler function can read one parameter which is number of milliseconds between system start and the moment when handler was fired. It means it's basically current tick count (if the handler is not delayed in lua queue).

Periodic calls

If you need to call function in a loop after some interval, just put another lmc_set_timer call as the last command in your handler. For example

function oneSecondTimerHandler(ts)
  print('Timer alarm at time stamp '..ts..'.')
  lmc_set_timer(1000, oneSecondTimerHandler)
end

lmc_set_timer(1000, oneSecondTimerHandler)

Be careful

Make sure you do not set timers to run too often and their handlers do not run too long. In that case your lua queue can overflow and handlers won't be executed. If you're debugging your code and timers get wild use reset button in main window's top bar. This reset will clear all timers and also reset lua interpreter.

Below just small example how to kill your lua queue. It creates 70 timers which start in a way that lua handler is started every second. As the handler's processing takes 5 seconds and handlers calls are serialized the execution queue gets full and some handlers are not called.

function timerHandlerWhichRuns5seconds(ts)
  print('STARTED timer alarm at time stamp '..ts..'.')
  lmc_sleep(5000)
  print('FINISHED handler which started at time stamp '..ts..'.')
end

for i=1,70 do
  lmc_set_timer(1000*i, timerHandlerWhichRuns5seconds)
end

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.