What I am trying to do is generate a random value for different colors in a sessionStorage object and have it stay the same for the duration of the session (until the window is closed). Here is my code so far:
var colorsArray = ["string", "myString", "something"];
var randomNumber = Math.floor(Math.random() * colorsArray.length);
var color = colorsArray[randomNumber];
if(typeof(Storage) !== "undefined") {
sessionStorage.color = color;
} else {
console.log("Session storage is not supported");
}
console.log(sessionStorage.color);
So, this works as far as saving a random value into the sessionStorage object, but it generates a new value each time. What I want to do is set a value randomly on load and save that value for the duration of the session. When the browser is closed, it would reset.
My question is, how would I save a randomly generated value into the sessionStorage object for the duration of the session and reset it when the browser is closed??
There is one and only one problem with this code:
You keep re-setting the color saved in your sessionStoreage every time you run the page.
You need a line like this: