Repeated calculation with different parameters upon MouseUp Button action

New Here ,
Oct 31, 2017

Copy link to clipboard

Copied

I need a repeated calculations with different array parameters upon the same button MouseUp activation.

i.e. when the user press-leave the mouse button I want the Javascript to take 2 values from an array, perform some calculations and send to TextFields. Upon next iteration (a user induced a MouseUp again) the code should move to the next 2 values within the array and so on.

What is the best way to implement it? Should I set any global variable (counter)?

Thanks a lot!

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

So let's say you have an array of numbers and each time the button is clicked you want to multiply two of them.

You could use a global variable, but then you need to make sure that that option is enabled in the preferences, so it might be better to just use a doc-level variable, or even a (hidden) text field, if you want to maintain the position even after the file is closed.

At the doc-level you can have this code:

var position = 0;

var nums = [13, 3, 63, 2, 76, 10];

As the MouseUp event of the button you can use this code:

this.getField("Text1").value = nums[position] * nums[position+1];

position+=2;

if (position>=nums.length) position=0; // reset when reaching the end of the array

TOPICS
Acrobat SDK and JavaScript

Views

203

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Repeated calculation with different parameters upon MouseUp Button action

New Here ,
Oct 31, 2017

Copy link to clipboard

Copied

I need a repeated calculations with different array parameters upon the same button MouseUp activation.

i.e. when the user press-leave the mouse button I want the Javascript to take 2 values from an array, perform some calculations and send to TextFields. Upon next iteration (a user induced a MouseUp again) the code should move to the next 2 values within the array and so on.

What is the best way to implement it? Should I set any global variable (counter)?

Thanks a lot!

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

So let's say you have an array of numbers and each time the button is clicked you want to multiply two of them.

You could use a global variable, but then you need to make sure that that option is enabled in the preferences, so it might be better to just use a doc-level variable, or even a (hidden) text field, if you want to maintain the position even after the file is closed.

At the doc-level you can have this code:

var position = 0;

var nums = [13, 3, 63, 2, 76, 10];

As the MouseUp event of the button you can use this code:

this.getField("Text1").value = nums[position] * nums[position+1];

position+=2;

if (position>=nums.length) position=0; // reset when reaching the end of the array

TOPICS
Acrobat SDK and JavaScript

Views

204

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 31, 2017 0
Most Valuable Participant ,
Nov 04, 2017

Copy link to clipboard

Copied

So let's say you have an array of numbers and each time the button is clicked you want to multiply two of them.

You could use a global variable, but then you need to make sure that that option is enabled in the preferences, so it might be better to just use a doc-level variable, or even a (hidden) text field, if you want to maintain the position even after the file is closed.

At the doc-level you can have this code:

var position = 0;

var nums = [13, 3, 63, 2, 76, 10];

As the MouseUp event of the button you can use this code:

this.getField("Text1").value = nums[position] * nums[position+1];

position+=2;

if (position>=nums.length) position=0; // reset when reaching the end of the array

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 04, 2017 0
New Here ,
Nov 04, 2017

Copy link to clipboard

Copied

Thanks for the answer. I thought that every doc-lever var is global, but from your answer I see that you differentiate them... could you please elaborate (which option should be enabled)??

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 04, 2017 0
Most Valuable Participant ,
Nov 05, 2017

Copy link to clipboard

Copied

A "global" variable means something very specific in Acrobat, namely, a variable that was defined using the global object.

Such variables are accessible by all documents in the application, and can even be set up to be persistent beyond the current session, ie you could close Acrobat down and the next time you restart it the variable will still exists, with its last value.

A doc-level variable is accessible to all scripts that are located within that document, but it's not "global".

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 05, 2017 1
New Here ,
Nov 05, 2017

Copy link to clipboard

Copied

where can I read about this "global" object? I failed to find anything

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 05, 2017 0
Adobe Community Professional ,
Nov 05, 2017

Copy link to clipboard

Copied

Look at the Acrobat JavaScript Reference.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 05, 2017 2