Programming Guidelines

<< Click to Display Table of Contents >>

Navigation:  JavaScript >

Programming Guidelines

In this chapter we want to provide some general programming guidelines for JavaScript programming with our devices.

There is no absolute truth, no one best way, but avoiding the worst ways is a good start.

 

Check if a functionality exists

Before implementing something complex, check thoroughly if there is not a functionality implemented in the program that does what you want to do. If in doubt, contact your vendor for support and consulting.

 

No synchronization between PClient and JavaScript

JavaScript has no direct access to any variables or data in the PClient. Use our functions to read and write variable values and object properties.

But even doing that does not establish a connecttion between PClient and JavaScript. Reading a PClient variable in JavaScript is like taking a momentary snapshot.

 

No time in a script

It is not possible to spend any "time" in a script, i.e. wait for a certain event. You cannot set a variable, then wait 5 seconds for the outcome of the change, then continue in the script. If you need a time component for e.g. a gradual value change, you need to use a cyclic script, e.g. on the event OnPageRepeat, and increment a variable with each run.

 

Don't put everything in OnProjektRepeat

It seems easy and convenient to just put all your code in the script OnProjektRepeat, this way everything will be executed all the time and all JavaScript variables will always be available.

But such a script will become very big, and all code will be executed all the time, which will slow down your project.

 

Use the right events for the right code

Always consider which event is the right event for the code you want to execute.

E.g. if you have a calculation dependent of a variable, put the code to the event OnValueChangeByOwner of that variable.

 

Avoid using "global" JavaScript variables

It is possible to declare JavaScript variables globally, so that they will be available in other scripts. But this should be avoided as much as possible. It is safer to store values in PClient variables, read them at the beginning of the script and write them back after modification in the script.

 

Don't combine commands

We've had crashes and problems sometimes with code lines like

 

setVariableValue(“test”,(getVariableValue(“test”)+1);

 

In a million runs this might crash the PClient one time. Don't program like that, it does not save any resources over spreading it out like this:

 
var test_js = getVariableValue ("test");

test_js = test_js + 1;

setVariableValue ("test",test_js);