I have a random increase for a bunch of variables, I used setIntervel() to have something like a live update for the variables but they don't seem to update:
const no= document.getElementById("no");
const ni= document.getElementById("no");
const nu= document.getElementById("no");
const ny= document.getElementById("no");
const nt= document.getElementById("no");
const visit = document.getElementById("visited");
let a = sessionStorage.getItem("a") ? parseInt(sessionStorage.getItem("a")) : 1;
let b = sessionStorage.getItem("b") ? parseInt(sessionStorage.getItem("b")) : 1;
let x = sessionStorage.getItem("x") ? parseInt(sessionStorage.getItem("x")) : 1;
let y = sessionStorage.getItem("y") ? parseInt(sessionStorage.getItem("y")) : 1;
let z = sessionStorage.getItem("z") ? parseInt(sessionStorage.getItem("z")) : 1;
let display;
no.onclick = function(){
sessionStorage.setItem("a", a + 1);
window.location.href = "no.html";
}
ni.onclick = function(){
sessionStorage.setItem("b", b + 1);
window.location.href = "ni.html";
}
nu.onclick = function(){
sessionStorage.setItem("x", x + 1);
window.location.href = "nu.html";
}
ni.onclick = function(){
sessionStorage.setItem("y", y + 1);
window.location.href = "ny.html";
}
nt.onclick = function(){
sessionStorage.setItem("z", z + 1);
window.location.href = "nt.html";
}
function update(){
if(a > b && a > x && a > y && a > z){
display = "Popular Now: no";
visit.textContent = display;
}
else if(b > a && b > x && b > y && b > z){
display = "Popular Now: ni";
visit.textContent = display;
}
else if(x > a && x > b && x > y && x > z){
display = "Popular Now: nu";
visit.textContent = display;
}
else if(y > a && y > b && y > x && y > z){
display = "Popular Now: ny";
visit.textContent = display;
}
else if(z > a && z > b && z > x && z > y){
display = "Popular Now: nt";
visit.textContent = display;
}
}
setInterval(() => {
let i = Math.floor(Math.random() * 5 + 1);
if (i === 1) {
sessionStorage.setItem("a", parseInt(sessionStorage.getItem("a") || 0) + 1);
} else if (i === 2) {
sessionStorage.setItem("b", parseInt(sessionStorage.getItem("b") || 0) + 1);
} else if (i === 3) {
sessionStorage.setItem("x", parseInt(sessionStorage.getItem("x") || 0) + 1);
} else if (i === 4) {
sessionStorage.setItem("y", parseInt(sessionStorage.getItem("y") || 0) + 1);
} else {
sessionStorage.setItem("z", parseInt(sessionStorage.getItem("z") || 0) + 1);
}
update();
}, 5000);
The live update should occur right after the values increase meaning that it isn't using outdated values, yet I have to manually reload the page to see the change, and if anybody can offer a more random way of adding to the values I would like to know it, also don't mind the weird variable and button names, it is not something I would like to share online, Thanks.
I was expecting a live update for the popular now part
Your fairly "verbose" code can be written a lot shorter if you leave out all the repetitions:
The above snippet does not use local storage as this is neither supported on Stackoverflow nor is it useful for the action you require here. If you still want this feature, then you should uncomment the local storage command in the
onclickfunction definition.Maybe the expression
deserves a short explanation: it uses the
Array.reduce()method that loops over all elements of thebtnsarray and compares each element's.textContentattribute value with the one in the accumulator variablea. Thaavariable is initially set to the first element of thebtnsarray and is assigned the result of the ternary operator expression(+c.textContent>+a.textContent?c:a). So, if the current element's (c) value is higher than the one stored inait will be assigned toa. At the end of the.reduce()loop the element stored inais returned and its attribute.idis then used for the outer expression stating the most popular id.