Jesper Gjødvad
Jesper Gjødvad

Reputation: 23

LocalStorage Save Javascript

My localstorage save and load won't work I can't load or save. Can you please help me out? I don't know whether it is the variables or functions that has been put up incorrectly The functions are being called by HTML buttons that goes "onclick" and has ID's

    var faze = 0;
function fazeClick(number){
    faze = faze + number;
    document.getElementById('faze').innerHTML = faze;
}
var mntDew = 0;
function buyDew(){
    var mntDewCost = Math.floor(10 * Math.pow(1.1,mntDew));
    if(faze >= mntDewCost){
        mntDew = mntDew +1;
        faze = faze - mntDewCost;
        document.getElementById('mntDew').innerHTML = mntDew;
        document.getElementById('faze').innerHTML = faze;
    };
    var nextCost = Math.floor(10 * Math.pow(1.2,mntDew));
    document.getElementById('mntDewCost').innerHTML = nextCost;
};
window.setInterval(function(){
    fazeClick(mntDew);
}, 1000);

function save(){
    localstorage.setItem.faze;
};
function load(){
    document.getElementById("faze").innerHTML = localstorage.getItem.faze;
};
document.write(faze);

// <button type="button" onClick="buyDew()">Buy Mountain Dew</button> #buyCursor
//Mountain Dew: <span id="mntDew">0</span><br/> #cursors
//cost: <span id="mntDewCost">10</span> #cursorCost

Upvotes: 0

Views: 94

Answers (2)

Jos de Jong
Jos de Jong

Reputation: 6819

Check out the syntax for loading/saving values in localStorage

http://www.w3schools.com/html/html5_webstorage.asp

You can either use:

localStorage.setItem('faze', newFazeValue);      // save
var newFazeValue = localStorage.getItem('faze'); // load

or

localStorage.faze = newFazeValue;     // save
var newFazeValue = localStorage.faze; // load

Upvotes: 0

Beri
Beri

Reputation: 11620

You got one letter wrong, it is spelled localStorage:

http://diveintohtml5.info/storage.html

var foo = localStorage.getItem("bar");
// ...
localStorage.setItem("bar", foo);

I would mostly advise to wrap your storage in a class, so that you would create a bridge pattern. And separate place of storage from your code logic, it will be easyer to change it so other type of storage in the future.

Upvotes: 1

Related Questions