I am using Svelte and trying to make a game where when you click it will add a number, it works fine at first, but I make sure to save there data, for now im just storing it in localeStorage
, now here is when the problem comes in. When I click the button now it will add a digit instead of adding just one to the number.
This is a bit of a weird question because it is oddly specific. I have looked up multiple things, and I cant seem to find this question has been asked before so I am now asking, Ive spent about 30 minutes researching, and nothing has came up.
What I think is happening is that the localeStorage is storing it as a string and not a number even though I have it in a :number
to make sure that the variable will always be a number.
let clicks: number = 0;
let cps: number = 0;
let cpc: number = 1;
let cm: number = 1.00;
function handleClick() {
const newClicks = cpc * cm;
clicks += newClicks;
window.localStorage.setItem('clicks', clicks);
}
window.addEventListener("load", function() {
let sClicks = window.localStorage.getItem('clicks');
clicks = sClicks;
}, false);
this is inside of my <script>
and in my html I have a simple <button on:click={handleClick}>{clicks}</button>
Im going to try to describe the problem a little more detail if you dont understand, when ever I click the button it will add one so it will go like this,
1, 2, 3, 4, 5, 6
now when I refresh the same number is there however when I click now it will go
61, 611, 6111, 61111, 611111
and so on. And it will still update the value in the localeStorage to be 6111
for example.
Thank you for any help.