Notifications
Clear all

Run a script when a variable changes?

5 Posts
4 Users
2 Reactions
35 Views
Posts: 3
Member
Topic starter
 

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!


 
Posted : 03/09/2026 4:00 p.m. CEST
Dominik
Posts: 27
Member Admin
 

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.


 
Posted : 09/09/2026 1:39 p.m. CEST
MarAlz-RW
Posts: 17
Member
Posts: 3
Member
Topic starter
 

That was exactly what I was looking for. I will try it with OnVariableChanged.

Thanks both of you, that helps a lot!


 
Posted : 10/09/2026 12:38 p.m. CEST
Posts: 1
Member
 

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


 
Posted : 11/09/2026 7:23 a.m. CEST