user3058477
user3058477

Reputation: 101

store and retrieve game state using HTML5 DOM storage and JSON

I am using helper functions to store and retrieve game state using HTML5 DOM storage and the JSON features built into ECMAScript5, my code is:

function saveState(state) {
    window.localStorage.setItem("gameState",state);
}

function restoreState() {
    var state = window.localStorage.getItem("gameState");
    if (state) {
        return parse(state);
    } else {
        return null;
    }
}

but anyhow I am not getting desired output, as i am new to JSON its hard to resolve. HELP please !

Upvotes: 2

Views: 202

Answers (1)

Shivanshu
Shivanshu

Reputation: 1269

Try below code:

function saveState(state) {
    window.localStorage.setItem("gameState", JSON.stringify(state));
}

function restoreState() {
    var state = window.localStorage.getItem("gameState");
    if (state) {
        return JSON.parse(state);
    } else {
        return null;
    }
}

Upvotes: 2

Related Questions