Copy link to clipboard
Copied
The cNum is taken from my.json.
Is it a string? But then it can participate in arithmetic.
“cNum”: 24.
Here (cNum>6) seems to work, even though checkCode(); has been executed, cNum still increases by 1.
cNum keeps increasing whenever you click on it!
And it will add two 1's (+1+1),I checked elsewhere without the same setting.
if (cNum <= 6) {
readwriteJSON();
}
else if (cNum >6) {
checkCode();
}
function readwriteJSON() {
var myJSON = readFile(pathToMyJSONFile);
if (!myJSON)
return alert('Could not read JSON file.');
myJSON = JSON.parse(myJSON);
cNum = myJSONObject.cNum;
myJSON.cNum = cNum + 1;
alert(cNum);
var f = writeFile(pathToMyJSONFile, JSON.stringify(myJSON, undefined, '\t'));
}
Copy link to clipboard
Copied
Now I only keep it outside
if (cNum > 6) {
checkCode();
}
And putting the following into readwriteJSON(){ It is seems to work fine.
But I still can't figure out the front side.
if (cNum <= 6) {
myJSON.cNum = cNum + 1;
alert(cNum);
Copy link to clipboard
Copied
@dublove you are confusing things.
In this line
if (cNum <= 6) {
you are referring to an undeclared variable, which will give an error, unless you declare it earlier like this:
var cNum = 0
I'm assuming you have done that earlier in your code.
But the in the readwriteJSON function you increment the object—hopefully a number—that is stored in the myJSON variable's cNum property. And then you show the variable cNum in an alert box. But because these are totally different values, it makes no sense.
In your readwriteJSON function, change these lines:
cNum = myJSONObject.cNum;
myJSON.cNum = cNum + 1;
to
cNum++;
myJSON.cNum = cNum;
(Note that cNum++; is the same as cNum = cNum + 1;)
Also, very important to note is that you can access cNum from the readwriteJSON function only because they are in the same variable scope—in some cases you can do it that way.
- Mark
Copy link to clipboard
Copied
But my cNum isn't 0.
It has an initial value.
Its value is cNum = myJSONObject.cNum;
Copy link to clipboard
Copied
@dublove most of your problems lately are that you don't understand basic javascript. I would strongly recommend you spend some time studying basic ECMAScript 3 (ES3) javascript which is almost identical to ExtendScript. Please find a tutorial or two.
Specifically need to understand about variables and variable scope, objects, properties, functions and passing values to and from functions.
Your questions are becoming very muddled because you don't share the whole code and your partial code snippets make no sense. We can't be much help if this is the way.
- Mark
Copy link to clipboard
Copied
Hi m1b.
I'll definitely look into it when this long busy period is over.
Copy link to clipboard
Copied
Hi m1b.
In my code in post #1, readwriteJSON(); is executed twice.
Now I realized that without readwriteJSON(); the code executes as usual.
Without the call, the function readwriteJSON(); function also executes ......
Why is that?
Thank you very much, I found another page hiding a readwriteJSON();
is it called again.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now