Adding a high score to local storage

后端 未结 5 1751
轻奢々
轻奢々 2021-01-06 16:37

I want to add a high score to my game. Right now I have this:

var score = 0;
var highscore = 0;

The score variable works but whenever I get

5条回答
  •  隐瞒了意图╮
    2021-01-06 16:58

    Try this:

    var score = 0;
    var highscore = localStorage.getItem("highscore");
    
    if(highscore !== null){
        if (score > highscore) {
            localStorage.setItem("highscore", score);      
        }
    }
    else{
        localStorage.setItem("highscore", score);
    }
    

提交回复
热议问题