Hi everyone,
I need to run some JavaScript when a PLC value changes.
I could check the value every second with a timer, but I guess there is a better way for this. Is there an event I can use when the variable changes? Something like a "variableChanged" event or something similar?
Thanks!
Hi Sebi,
yes, you can assign a script directly to the OnVariableChanged event of a variable.
The script is only executed when the value changes, so you don't need a timer. You can also use the event arguments to access the new and previous value.
It's very easy to do using the following board tools:
German: https://docs.quickhmi.com/dokumentation/definition/events/#Events.onVarChanged
English: https://docs.quickhmi.com/en/dokumentation/definition-en/events/#Events.onVarChanged
and
German: https://docs.quickhmi.com/dokumentation/definition/variable-change-functions/
English: https://docs.quickhmi.com/en/dokumentation/definition-en/variable-change-events/
That was exactly what I was looking for. I will try it with OnVariableChanged.
Thanks both of you, that helps a lot!
Hi there, this is the Java Script I use to monitor a variable change ...it converts whatever comes from the PLC to an integer (in my case to use for a rotation animation)
Events.onVarChanged("PLC.C_Angle",
function(args) {
var angle =0;
var val2 =0;
angle = mainInterface.getVariable("PLC.C_Angle");
val2 = Math.round(angle);
mainInterface.setVariable("Intern.C_Angle_Int", val2);
});
It is expecting a defined External tag C_Angle, and a defined Internal tag C_Angle_Int
cheers Ross

