0

So I was bored and messing around with javaScript and here's my current code:

JS

var Money = 0;
var U1Amount = 0;

function mButton()
{
    Money += 1;
    document.getElementById('money').innerHTML=Money + "$";
}
function buyU1()
{
    if(Money < 30)
        {
            document.getElementById('updates').innerHTML="You do not have enough money.";
        } else {
            Money -= 30;
            U1Amount += 1;
            document.getElementById('money').innerHTML=Money + "$";
            document.getElementById('updates').innerHTML="You have successfully bought Upgrade 1";
            resetUpdates();
        }
}
var interval = setInterval(gMoneyU1, 1000);
function gMoneyU1()
{
    var calc = 5 * U1Amount;
    Money += calc;
    document.getElementById('money').innerHTML=Money + "$";
}
function resetUpdates()
{
    setTimeout(function(){document.getElementById('updates').innerHTML="";}, 10000);
}
</script>

HTML

<body>
Generated something: <span id='money'>0$</span>
<br />
<span id='updates'></span>
<br />
<button onClick='mButton()'>Generate something.</button>
<br />
<button onClick='buyU1()'>Buy upgrade 1. ($30)</button>
</body>

I have tried to google and found out about some localStorage.setItem and stuff, but I'm unsure how to use it. If someone could explain it I'd really appreciate it. :)

Thanks in advance.

vinczemarton
  • 7,756
  • 6
  • 54
  • 86
prk
  • 3,781
  • 6
  • 17
  • 27

2 Answers2

0

I guess you must have gone through this already but I would like to suggest it. For a quick overview go through http://www.w3schools.com/html/html5_webstorage.asp and for a detailed look have a look at http://diveintohtml5.info/storage.html. I think it will server your purpose. It's not very big.

0

@Voltrone suggested Lawnchair and while that's a good library for offering fallback implementations of storage (for example if you are on an older browser without localStorage), I would suggest you use lscache instead: https://github.com/pamelafox/lscache

It's going to make the storage you need to do simpler (just hand it an object to store and a key under which to store it) and it allows you to set how long you want the stuff you store to last if you need to do that.

John Munsch
  • 19,530
  • 8
  • 42
  • 72