3 ms·
You have a global variable, count, which is involved in the calculation and isn't being updated in your clear. Your updated code in the comments does that. Howe
by jonnycomputer 4y ago
You have a global variable, count, which is involved in the calculation and isn't being updated in your clear. Your updated code in the comments does that. However, it doesn't make a lot of sense to do that, since you can just use the JavaScript variable. However, localStorage and sessionStorage persist on page reload, so it's useful in that scenario. Therefore to make use of that capability, you should do something like this:
function decreaseCounter() {
let count = parseInt(window.localStorage.getItem("count"));
count = count - 1;
window.localStorage.setItem("count", count);
}
all values stored in localStorage and sessionStorage are strings, so you have to convert to a number. You'd probably want a try/catch in there somewhere if count is not yet defined in the store, unless that possibility is dealt with elsewhere in your code. Also, the counter might need to check for boundary conditions (e.g. it can be negative here). But that is beyond the scope of your question.