语言

Menu
Sites
Language
Store user settings

Hi,

Could anyone suggest me a proper way to store user options/setting? These are loaded once the app starts and updated by user activity.

Say I want to store/update a highscore list for a game.

 

Thanks

编辑者为: Brock Boland 17 3月, 2014 原因: Paragraph tags added automatically from tizen_format_fix module.

响应

2 回复
Marco Buettner
U can use different ways You can use IndexedDB, so you can create a data bank to storage your highscores. Other method is you use LocalStorage. use localStorage.setItem(key, value) to save the data. use localStorage.getItem(key) to load the data. Use an array of maps which includes name and highscore of the players. example: var highscores = new Array(); example array with values highscores = [{"name":"player1", "highscores":"100"}, {"name":"player2", "highscores":"80"}] localStorage.setItem("highscores", highscores); highscores = localStorage.getItem("highscores"); highscores = "[{"name":"player1", "highscores":"100"}, {"name":"player2", "highscores":"80"}]" The function updateHighscore(highscore) you have to read your array. Maybe it is the first highscore, so currentHighscores = null function updateHighscore(highscore) { var currentHighscores = localStorage.getItem("highscores") if(currentHighscores == null) { var name = $('#username').val(); var highscore = $('#highscore').val(); var newHighscore = {"name":name, "highscore": highscore) var setNewHighscore = highscores.push(newHighscore) localStorage.setItem("highscores", setNewHighscore); } else { // write method to check is the highscore higher oder lower than the other highscores in your array :) // dont forget to convert your the highscore string back to array. } } Hint: localStorage save the value always as a string. if you load your storage you get a string, maybe you have to convert it back to an array.
Varsamis
Thank you Marco!