How to set a timeout?

Is there a way to have a timeout function in the script somehow?
Thanks in advance!

1 Like

Hi, yup there is. Check the Timer object in the Lua extension - Timer docs.

Thank you!
Can you provide an example how I can delay the execution inside a function like that:

task a
timeout 10
task b

I tried it in different ways without luck

I built a workaround for that now:

  • a queue that gets filled with functions to execute
  • the timer pops and executes the functions

That works, but seems a bit complicated :slight_smile:

1 Like

what are you trying to achieve? Repetitive execution of a set of functions? Please let me know. Electra uses a test scheduler internally. Maybe, what you want to do could be done by providing an access to the scheduler API.

I have to send two midi CCs:

  • value 127 first
  • then I have to wait some milliseconds
  • value 0

This is needed, because my device doesn’t recognize the gate signal otherwise.

Hi, in the device settings there is a parameter called “rate”. Have you tried it? It puts delays between midi messages. Set the value somewhere between 5 and 100 and test if it solves your problem.

I am creating and sending midi messages in a custom function that is called by pressing a (virtual) pad.
So I guess that this setting does not affect code that gets executed in lua functions.

Don’t know if that affects lua code as well.

In any case, here is the way I resolved something similar, where I needed code to wait (by invoking the enableTimer function below), which after the time period called the onTick function that continued the desired process.

function enableTimer(period) 
   myCounter = 0
   timer.setPeriod(period)
   timer.enable()

end

function timer.onTick()
   if myCounter ==1 then 
     timer.disable()
     Other actions
  end
  myCounter = myCounter + 1
end

Yeah I began with something similar but then I needed to call different functions in the onTick function (depending on which button I press)

So my solution was a queue where I push the function to execute and on every tick a function gets popped and executed. That works. But I am curious if there exists an easier solution.

Quick thing might be for each object (button press) you set a global variable (declared at the top of the code outside any function) with a unique number. Inside the onTick(), if it is time to execute a function, it checks the global and uses an “if” statement (or case/switch - does LUA have this?) to decide which function to call.

You can prevent multiple calls if you set the initial value of the variable to some number that is not associated with a function (say 0) and then at the bottom of the function that is called, it sets the global back to 0.

1 Like