简体   繁体   中英

Storing JavaScript values forever

So I am making a program that generates 360,000 different values (0s, 1s) and then checks to see if there's anything special about the odds

if (zeros < 49.00) {
  console.log("Wow");
} else if (ones < 49.00) {
  console.log("Wow");
} else if (zeros == 50.00 && ones == 50.00) {
  console.log("Perfectly balanced as all things should be");
} else {
  // If there is nothing special in the odds just reload
  location.reload();
}

Now this is working perfectly fine, but now I want to store:
Times the program has run,
Times it's been 50/50,
Times 0s or 1s have been < 49%,

My genius mastermind thought it'd be a great idea to just zerosMin++ But that didn't work because every time the program runs this value resets.
But how can I store these "statistics", preferably in another file that doesn't reset?

You can store the variable data in local storage if no backend is involved. Local storage will retain its values until its either manually cleared or the site data is cleared from browser itself. The other available option is to use session storage but that will be gone once you reload or open the same page in new tab.

if (typeof(Storage) !== "undefined") {
  // Code for localStorage/sessionStorage.
  if (localStorage.zerosmin) {
    localStorage.zerosmin) = Number(localStorage.zerosMin) + 1;
  } else {
    localStorage.zerosmin = 0;
  }
} else {
  // Sorry! No Web Storage support..
  // Some vintage version of IE maybe
}

  1. You can store variables in cookies
  2. You can send them to backend (post or get request to php, node.js, asp etc.), and read whenever you need them again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM