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.
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.");
}
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
}
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
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.
Copy link to clipboard
Copied
Something like this?
var prevA;
function goThere(newA) {
prevA = newA;
this.pageNum = newA;
}
function goBack() {
this.pageNum = prevA;
}
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.)
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.");
}
Copy link to clipboard
Copied
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.
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,
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).