Problems using getItem and storeItem - any suggestions

I am attempting to develop a sketch that will collect information and store it for use in subsequent reloads of the sketch. Given the information related to the “storeInfo” and “getInfo” operations I was under the impression that the following sketch.js would accomplish that and the “counter” would accumulate with each reload. It doesn’t. I have run it with and without the use of a localhost and with Chrome and Safari on a Mac. This is not much more than a small expansion of the Examples provided on the p5.js website.

It never appears to “find” the value for “myCounterOld” on a reload of the sketch - Isn’t that what it is supposed to be able to do? Any ideas"

// Click the mouse to change the displayed counter
 // Once you have changed the counter
 // it is supposed to stay changed even when you
 // reload the page - but it doesn't.

let myCounterOld;
let myCounter;

function setup() {
   createCanvas(400, 300);
   background(122);
   getItem('myCounterOld');
   if (myCounterOld === undefined) {myCounter =0; myCounterOld=0;}
   else {myCounter = myCounterOld;}
 }

function draw() {
   rectMode(CENTER); fill(222); rect(width/2, height/2, .9*width, .9*height);
   textAlign(CENTER); textSize(16); fill(0); text(myCounter, width/2, height/2);
   textAlign(CENTER); textSize(16); fill(0); text(myCounterOld, width/2, 3*height/4);   
 }
 //If you press the mouse, the counter increments.
function mousePressed() {
   myCounter = myCounter + 1;
   myCounterOld=myCounter;
   storeItem('myCounterOld', myCounterOld);
 }

1 Like

myCounterOld = getItem('myCounterOld');

1 Like

Got it. I thought I had tried that but obviously I had not done something correctly.
That works as advertised. Once again the Forum saves me!
Thank you for this amazingly fast solution.
Cheers and stay safe where ever you are!

1 Like