• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Use previous value of "A"

Community Expert ,
Feb 19, 2019 Feb 19, 2019

Copy link to clipboard

Copied

Hi JavaScript experts.

I need to use the previous value of "A" in a function but my brain isn't enought logical to find an easy way by itself.

function myFunction(A) {

    // 1. blablabla, use A in one or several lines

    // 2. here I need to keep the value of A for the next time the function will be called

}

Thank you for your help.

TOPICS
Acrobat SDK and JavaScript

Views

809

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
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 20, 2019 Feb 20, 2019

So it's not the previous value of A, it's the one before that...

You can do it like this:

var prevA = [];

function goThere(newA) {

    prevA.push(newA);

    this.pageNum = newA;

}

function goBack() {

    if (prevA.length>=2) this.pageNum = prevA[prevA.length-2];

    else console.println("can't go back.");

}

Votes

Translate

Translate
LEGEND ,
Feb 19, 2019 Feb 19, 2019

Copy link to clipboard

Copied

You can save a value in a variable but (a) don't use "var" and (b) define it OUTSIDE your function first.

Info about variable scope in JavaScript: Demystifying JavaScript Variable Scope and Hoisting

mySavedVariable = -99999999 ;

function myStuff(A)

{

  // do things

  if ( A != mySavedVariable ) { // do things if A is different from last time

  }

  mySavedVariable = A ; // save A for next time

}

Votes

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
community guidelines
LEGEND ,
Feb 19, 2019 Feb 19, 2019

Copy link to clipboard

Copied

Another approach is to use a closure, which avoids the problem of a global variable like TSN suggested from being changed by code elsewhere. This subject can be a bit complicated, but there are plenty of tutorials out there, such as: JavaScript Function Closures

Votes

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
community guidelines
Community Expert ,
Feb 19, 2019 Feb 19, 2019

Copy link to clipboard

Copied

Thank you both but it does not meet my needs, I will try to explain myself better.

In fact there are 2 functions :

function goThere(A) {

this.pageNum = A;

}

//

function goBack() {

this.pageNum = B;

}

Where B is the previous value of A, not the latest value. Otherwise it will goBack on the current page.

Votes

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
community guidelines
Community Expert ,
Feb 19, 2019 Feb 19, 2019

Copy link to clipboard

Copied

Something like this?

var prevA;

function goThere(newA) {

    prevA = newA;

    this.pageNum = newA;

}

function goBack() {

    this.pageNum = prevA;

}

Votes

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
community guidelines
Community Expert ,
Feb 20, 2019 Feb 20, 2019

Copy link to clipboard

Copied

Something like this?

var prevA;

function goThere(newA) {

    prevA = newA;

    this.pageNum = newA;

}

function goBack() {

    this.pageNum = prevA;

}

No. This is the first test I made.

In this case goBack will always go to the current page, since prevA is always equal to newA.

What I want to achieve:

User clic goThere(4) to go to page 4.

User clic goThere(6) to go to page 6.

User clic goThere(9) to go to page 9.

User clics goBack ==> he should go to page 6

I just need one goBack, not several.

(Don't ask me why, but I cannot use app.goBack() for this document.)

Votes

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
community guidelines
Community Expert ,
Feb 20, 2019 Feb 20, 2019

Copy link to clipboard

Copied

So it's not the previous value of A, it's the one before that...

You can do it like this:

var prevA = [];

function goThere(newA) {

    prevA.push(newA);

    this.pageNum = newA;

}

function goBack() {

    if (prevA.length>=2) this.pageNum = prevA[prevA.length-2];

    else console.println("can't go back.");

}

Votes

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
community guidelines
Community Expert ,
Feb 20, 2019 Feb 20, 2019

Copy link to clipboard

Copied

LATEST

So it's not the previous value of A, it's the one before that...

That's what I meant when I wrote "Where B is the previous value of A, not the latest value".

But English is not my native langage.

In any case you are a genius, it works perfectly !

Thank you, and thank you all.

Votes

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
community guidelines
Community Expert ,
Feb 19, 2019 Feb 19, 2019

Copy link to clipboard

Copied

Hi JR, do you want the value of A saved permanently in the PDF, or just for the current session?

The easiest and most straight forward solution is to use a custom document property

Like this

function goThere(newA) {

    this.prevA = newA;

    this.pageNum = newA;

}

Now A is saved in a document level property.

If you want it saved permanently, then use a hidden field.

Cheers,

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

Votes

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
community guidelines
Community Expert ,
Feb 20, 2019 Feb 20, 2019

Copy link to clipboard

Copied

Hi JR, do you want the value of A saved permanently in the PDF, or just for the current session?

I want this value to be saved until user go to another page (when the value should be replaced by a newer).

Votes

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
community guidelines