Initial code is here:
https://www.makeuseof.com/localstorage-javascript/
It’s the project that I’m trying to understand. It’s the javascript that I’m concerned.
I tweaked the Javascript code to this:
var count = 0;
function increaseCounter() {
count += 1;
window.localStorage.setItem("count", count);
document.getElementById("score").innerHTML = count;
}
function decreaseCounter() {
count -= 1;
window.localStorage.setItem("count", count);
document.getElementById("score").innerHTML = count;
}
function clearCounter() {
window.localStorage.clear();
document.getElementById("score").innerHTML = "";
}
This is what I’ve tweaked
I don’t get the need for the commented line in this case. Why’re we using it? I by now of course know it’s needed. But can’t understand why.
I’m facing a problem
I increase count to 13.
I clear localstorage.
I click decrease score. Now, score starts to decrease from 12. So, storage isn’t being cleared right?
Why’s it like that?
Dry run in the above code seems fine.
Initial count=0
Click increase count button
count=1
Again click increase count button
count=2
Now I clear Counter, count should be 0.
I click decrease counter
count=-1
and so on.
What am I missing here?
The persistant storage works fine in your example.
One thing I noticed is that you don’t update the value of the count
variable after changing the localStorage. Or clearing localStorage…
You have ‘cleared’ the local storage… but the count
variable still remembers its original value
In clearCounter()
function, try setting count
to 0
, so that it is the same as the localStorage’s value
2 Likes
kirupa
December 17, 2022, 1:10am
3
+1 to what @TheWaste said. Also, welcome to the forums
2 Likes
I see.
var count = 0;
function increaseCounter() {
// var count = Number(window.localStorage.getItem("count"));
count += 1;
window.localStorage.setItem("count", count);
document.getElementById("score").innerHTML = count;
}
function decreaseCounter() {
// var count = Number(window.localStorage.getItem("count"));
count -= 1;
window.localStorage.setItem("count", count);
document.getElementById("score").innerHTML = count;
}
function clearCounter() {
window.localStorage.clear();
count = 0;
document.getElementById("score").innerHTML = "";
}
This code works now. But is there a point of using localStorage if I’m doing this? I asked this question to specifically understand the first line.
// var count = Number(window.localStorage.getItem("count"));
The line you have referenced:
// var count = Number(window.localStorage.getItem("count"));
Will write the 'localStorage’s value back into the the count
variable…
(Which is a good idea, and solves your exact problem!)
But remember: the purpose of LocalStorage is that the values ‘will be remembered’ after the browser is closed.
You only need to use LocalStorage if you need the value to be ‘remembered’ after you close the browser.
In most cases, an application/script does not need LocalStorage.
(Unless ‘remembering the value after closing the browser’ is needed. Typically it isn’t)
Regarding ‘is there a point of using localStorage’…
To clarify, localStorage is kind of ‘special’… it’s not something you ‘use everyday’
It goes beyond just writing a value to the memory to use later in the script…
The point of LocalStorage is to “Maintain Persistance” - if you close the browser, it remembers the values.
In most cases you don’t need that.
(Unless, of course, remembering the value after you close the browser is a vital function)
2 Likes