Skip to main content
Inspiring
November 3, 2020
Answered

JS calculations are slow

  • November 3, 2020
  • 3 replies
  • 3432 views

Hi, 

I made my first form and everything is working good. That was my first time creating a form and my first try with JS. I know thatmy calculations are porbably not optimal, but i'd like to figure out something without starting all over or make dramatic changes to my form. 

I'd like to know if there is a way for my calculations to be faster. 

There is a little delay when choosing an item in many fields but specifically in those fields :

Type de collage

Produit

I can see that the calculations are runing. It could be anoying for the customer to wait everytime he choose an item.

If someone has a clue for me it would be appreciated.

I am working on Adobe Acrobat Pro DC.

My form is in french. 

Here is the link to my form :

https://documentcloud.adobe.com/link/review?uri=urn:aaid:scds:US:2c98e0b3-8af5-4701-972d-85991c54ca48

Thanks, 

Kassandra 

This topic has been closed for replies.
Correct answer Thom Parker

So, I was able to switch a part of my calculations into validations scripts but there is one remaining that I don't know how to figure out.

 

This is the calculation of the "Découpe" field.

Basically, what I wanted from that calculation is to check the box " Dessin" when one of those field is not empty or when a specific radio button is at yes. 

If field Découpe or Assemblage-Dessus or Assemblage pattes is not empty

OR if Porteafaux is checked at YES

OR if field Moulurage value is Personnalisé

Then I want my Dessin checkbox to be checked

 

I don't know how to do it differently by using validation script in each field. Every try I made didn't work out well. My value was cleared by the choice made in my second drop down. Even if Decoupe field was not empty, when I cleared field Assemblage-Dessus, it just cleared Dessin box, but my field Decoupe was still filled.

More, I don't know how to add my radio button into that condition.

Wondering if you could help me again, 

Thanks 

 


Good for you!! It's well worth the effort to try different ways to script a behavior, even if it doesn't work out. However, since setting the  "Dessin" depends on several inputs, it is appropiate to use a Calculation Script, just not on the dropdown. Instead, put a new text field next to the "Dessin" checkbox, and use the calculation script on this text box. Make this text field hidden. This separates the code for the calculation from the source fields. 

 

Now you need to learn a bit about how to write calculations.

These short article can help:

https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm

https://www.pdfscripting.com/public/Checkboxes-and-Radio-Buttons.cfm

 

In this case the calculation isn't with numbers, but with boolean values.  

Here's the calculation script for the hidden text field next to the "Dessin" checkbox

 

 

var bDessinValue = (this.getField("Decoupe").valueAsString != "0")
                      || (this.getField("Assemblagedessus").valueAsString != "0") 
                      || (this.getField("Assemblagepattes").value != 0)
                      || (this.getField("Moulurage").value =="Personnalisé") 
                      || (this.getField("Porteafaux") == "Oui");

this.getField("Dessin").value = bDessinValue?"Yes":"Off";

 

 

 

3 replies

Bernd Alheit
Community Expert
Community Expert
November 4, 2020

You should also fix the script errors in the form. 

Inspiring
November 4, 2020

I don't know how to do that though.

It's my first time using JS, so I probably don't know how to use it correctly yet.

Bernd Alheit
Community Expert
Community Expert
November 4, 2020

Look at the JS console for the error messages. 

Thom Parker
Community Expert
Community Expert
November 3, 2020

BTW: sharing you document as a review is problematic. If you want us to examine it, it should be shared as a plain file. 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
November 3, 2020

Thanks for the advices.

I just didn't know how to share it plain.

Thom Parker
Community Expert
Community Expert
November 3, 2020

You should be able to add it to this thread by editing your first post. The edit will include a paper clip symbol for attaching a file. 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Thom Parker
Community Expert
Community Expert
November 3, 2020

Calculations are problematic because they are triggered every time any field value is changed, which means they are usually run many more times than necessary. In fact, it's quite easy to create huge cascades of unnecessary calculations. 

The solution is to reduce the number of calculation scripts. There are several methods for fixing this issue. 

1) Move anything that doesn't need to be a calculation into a different type of script. The obvious targets are any calculation that only depends on one input. This can probably be moved to a validation script on the source field. 

2) Test for the source of a calculation before proceeding. If the source is not one of the inputs to the calc, then don't run the code.

3) Make sure your calculations are not spawning other calculations. This happens when a calculation script directly changes a field other than itself.  If you have calculations that do this, then it should probably be done a different way. 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often