Deniz Altay Deniz Altay - 3 months ago 5
HTML Question

HTML5 Localstorage help needed

I'm fresh to JS and i was wondering how i could implement this scenario to my code.

Im creating a simple tapping game for android using html, css and js. Whenever my object gets clicked on, 10 points gets added to score. I need this score to saved locally. So when the game restarts the score is there loaded.

Here are the game code:

var score = 0;

$(document).ready(function() {

//When bug is clicked on
$("#bug").click(function() {
score = score + 10;
$('#score').html('Points: ' + score + '') + 10;

function showMenu() {
$("#bug").css("visibility", "hidden");
$("#bug").css("animation-play-state", "paused");

function resume() {
$("#bug").css("visibility", "visible");
$("#bug").css("animation-play-state", "running");

<script src=""></script>
<div class="header">
<div id="score">Points:</div>
<div id="menuheader" onclick="showMenu()">M</div>
<div id="menu">
<div id="start" onclick="resume()">resume</div>
<div id="bug"></div>
<div class="bottomwall"></div>
<div class="topwall"></div>
<div class="leftwall"></div>
<div class="rightwall"></div>



basic localStorage usage.

When you starting your script, you can test if localStorage.score is already set with:

var score = 0;
    localStorage.score = 0;
score = parseInt(localStorage.score);

then in your click event:

localStorage.score = score + 10