Question

if (typeof localStorage["BestScore"] === 'undefined') {
    localStorage["BestScore"] = 0;
    var maxScore=0;
}     
else {
    var maxScore= localStorage["BestScore"] 
}

This code tries to check if the local storage best score is undefined/no value and if it is it sets maxscore to 0 if it has a value it set maxscore to the value. For some reason this code is not working please help.

Était-ce utile?

La solution

this is what i use. first check that storage is possible then check that your object is there. so...

if (typeof (Storage) !== "undefined") {
    if (!localStorage.BestScore) {
        localStorage.BestScore = 0;
    }

    var maxScore = localStorage.BestScore; 
}

Autres conseils

You missed a ; on line 6.

if (typeof localStorage["BestScore"] === 'undefined') {
  localStorage["BestScore"] = 0;
  var maxScore=0;
}else {
  var maxScore= localStorage["BestScore"] ;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top