In control, it is often necessary for an action to be called at an interval to update or check information. This article explains how this works.
Setup
for such an action, you need an AutoInit, because this action has to be initialized again when the Lua core is rebuilt or started.
Read more on the topic AutoInit and how to build a module
Tipp
Use IntelliSense for creating the code
The query for an already existing interval is important so that several are not built up in the background.
if self.interval ~= nil then
pixc.getRoot().Utils.Timer.clearInterval(self.interval)
self.interval = nil
end
The function to set the interval looks like this.
self.interval = pixc.getRoot().Utils.Timer.setInterval(function functionToCall, int TimeInMilliseconds)
Assignment to a variable is important for later accessibility.
As an example, here is a module that only has code in the init action, that only executes one action.
The finished code looks like this.
if self.interval ~= nil then
pixc.getRoot().Utils.Timer.clearInterval(self.interval)
self.interval = nil
end
self.interval = pixc.getRoot().Utils.Timer.setInterval(
function()
self.triggerThisActionEverySecond()
end, 1000)
The action "triggerThisActionEverySecond()" is triggered at an interval of 1000 milliseconds.
Warning
Be careful with the timing, low values can have a serious performance impact!
Don't use a value below 10ms, it can cause a overload on actions to call.
Pixera 1.9.174 | 02. November 2023