Contact Us

CATEGORIES

Whoops…Nothing found

Try other keywords in your search

Interval-triggered actions/code

 0 Minutes

 0 Likes

 1946 Views

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

Tip

Use autocompletion for creating the code

 

The query for an already existing interval is important so that several are not built up in the backend. This code here check if the varibale self.interval exists and clears it if so.

if self.interval ~= nil then
	pixc.getRoot().Utils.Timer.clearInterval(self.interval)
	self.interval = nil
end

The function itself to set the interval is this one.

self.interval = pixc.getRoot().Utils.Timer.setInterval(function functionToCall, int TimeInMilliseconds)

Assignment to a variable is important for later accessibility. 
self.interval = pixc.getRoot().Utils.Timer.setInterval…..

As an example, this code in the init action, that only executes one action.

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.

 

 

Make sure to create an uninit action to have the option to disable the interval.
 


Pixera 1.9.174 | 02. November 2023 | J.B.

Was this article helpful?