Skip to main content
Known Participant
May 13, 2016
Question

How do I convert the following Document level JS into a script in the document?

  • May 13, 2016
  • 25 replies
  • 2721 views

Here is the DLJ (Document Level Javascript) I want to convert to a script in a field in the document:

function getRanksValue(doc, name) {

    var rank = doc.getField(name).value;

    var value = 0;

    if (rank < 1) {

        value = -25;

    } else if (rank > 20) {

        value = 10 * 5 + 10 * 2 + (rank - 20);

    } else if (rank > 10) {

        value = 10 * 5 + (rank - 10) * 2;

    } else {

        value = rank * 5;

    }    return value;

}

The following is the script in a "Consolidated Script" field, where line 2 has the script that calls that DLJ:

for (var i=0;i<115;i++)

  this.getField("Rank Bns."+i).value = getRanksValue(this, "Ranks"+i);

  for (var ii=0;ii<115;ii++)

  this.getField("Total Bns."+ii).value = Number(this.getField("Rank Bns."+ii).value) + Number(this.getField("Stat Bns."+ii).value)

  + Number(this.getField("Spec Bns."+ii).value);

  this.getField("Total Bns.44").value = Number(racialStatMods[this.getField("Race").value][10])

  + Number(this.getField("Rank Bns.44").value) + (this.getField("Stat Bns.44").value) + Number(this.getField("Spec Bns.44").value);

  this.getField("Total Bns.45").value = Number(racialStatMods[this.getField("Race").value][11])

  + Number(this.getField("Rank Bns.45").value) + Number(this.getField("Stat Bns.45").value) + Number(this.getField("Spec Bns.45").value);

  this.getField("Total Bns.46").value = Number(racialStatMods[this.getField("Race").value][12])

  + Number(this.getField("Rank Bns.46").value) + Number(this.getField("Stat Bns.46").value) + Number(this.getField("Spec Bns.46").value);

It works as is but I am thinking it might be a little faster if I were to convert the DLJ to work in place of the script in the second section of line 02 above where it calls the DLJ. It would remove the need to call the DLJ. I have been trying to figure this out not for a few hours and I have not gotten anywhere.

I hope someone can help me.

This topic is closed to new replies. Start a new post to keep the conversation going.

25 replies

Karl Heinz  Kremer
Community Expert
Community Expert
May 13, 2016

If you are trying to optimize the performance of your scripts, move more functionality to document level scripts: They get interpreted once during document startup, whereas a field level script needs to be interpreted every time it gets activated. A field level script that is just one line that calls a document level script can be interpreted and executed much faster than if you would move the whole script into your field event handler.

In addition to this performance advantage, your form will be easier to maintain when you are using document level scripts: If you need to make a change in your functionality - or fix a bug - you make this change once in the document level function, and you don't have to hunt down every instance of where you have a copy of this code in a field.

MadmaxneoAuthor
Known Participant
May 13, 2016

I have all the script in either a DLJ or in a consolidated script box. There are only a few fields with separate script in them, not enough to make a difference. That particular script I am trying to convert deals with fields that have a few different scripts that apply to them. I have a set of buttons that takes the numbers in one set of fields, either ("RanksA" +i) or ("RanksT" +i) and adds them to the ("Ranks" +i)  fields. When I click one of those buttons the calculation takes 5 mins or more, it is the slowest calculation on the document. An example is it takes the number from the ("Ranks" +i) fields and adds it to the ("RanksA" +i) then places that total back in the ("Ranks" +i) fields then clears out the ("RanksA" +i) fields. Consequently there are 114 fields of each of those. That particular button calculation is a function in a consolidated script. It works but I had to separate the calculation in groups because it is so slow and not all the fields have to be calculated when it needs to be done.

All of the calculations in this form are necessary and in many ways pertain to the ("Total Bns."+i) fields that total 115 fields like the others. I originally intended to use DLJ's for just about all the script but I discovered that some calculations do not work right in the DLJ. I originally put them all in separate fields. I then moved 95% of those calculations into a single consolidated script field. That consolidated script field has over 1000 lines of script in it and I tend to keep all the script compacted as much as possible,

The reason I am trying to do this is because part of the script is in a consolidated script field and that script sends a call to the DLJ to get specific numbers. I am thinking that if I can have it all in one script instead of having a call to the DLJ it may possibly speed it up some. I would be essentially taking out the middle man.....

MadmaxneoAuthor
Known Participant
May 13, 2016

I forgot to explain. The fields "Ranks", "Rank Bns.", "Total Bns.", and "Stat Bns." are a total of 114 fields each, and they correspond with each other as you can see in the script above. They also have various scripts associated with them.

EDIT: To simplify my question further I am trying to take this script where it calls the DLJ:

  1. for (var i=0;i<115;i++) 
  2.   this.getField("Rank Bns."+i).value = getRanksValue(this, "Ranks"+i); 

Where the DLJ is getRanksValue and it has the following script:

function getRanksValue(doc, name) {

    var rank = doc.getField(name).value;

    var value = 0;

    if (rank < 1)

  {value = -25;}

  else if (rank > 20) {value = 10 * 5 + 10 * 2 + (rank - 20);}

  else if (rank > 10) {value = 10 * 5 + (rank - 10) * 2;}

  else {value = rank * 5;}

    return value;}

I want to take the actual getRanksValue script and replace the script where it calls the DLJ in line 02 of the first script I posted..

I was thinking it would look something like this:

for (var i=0;i<115;i++)

  this.getField("Rank Bns."+i).value =

    var rank = this.getField("Ranks"+i).value;

    var value = 0;

    if (rank < 1) {value = -25;}

  else if (rank > 20) {value = 10 * 5 + 10 * 2 + (rank - 20);}

  else if (rank > 10) {value = 10 * 5 + (rank - 10) * 2;}

  else {value = rank * 5;};

But it does not work like it is. I am missing something here, probably several somethings......

Any help?

Legend
May 13, 2016

I think if you hope to speed it up by just replacing a call with inline code you will be disappointed. You can probably do millions of calls a second. In fact, it may slow it down.  So if that's your only motivation, I wouldn't bother.

Otherwise, I note that you have for ( ... ) statement   where you probably want a compound statement i.e. { something ; something ; ... }

You can write

for ( ... )

  do this ;

  do that ;

but it will only do "this" inside the loop. I have done this many times, believing my layout (it's indented, right!) rather than properly using { } characters

for ( ... )

{

  do this ;

  do that ;

}

This is nearly as popular a trap as writing

if  ( I = 1 ) ...

instead of

if ( I == 1 ) ...

MadmaxneoAuthor
Known Participant
May 13, 2016

I am not sure I follow. I use the "{ }" where I need to in the script and "( )" where they are required.

Could you elaborate a little more?