好的,我創建了一個使用localStorage的HTML5 canvas遊戲。我有localStorage完美的工作,但我不確定它遵循正確的標準,或者如果它完全正確,我已經寫了它。HTML5 localStorage:我的代碼是否遵循正確的標準?
//check if storage is set already, if not set the variable
function checkLocalStorage(){
if (localStorage.timesPlayed){
timesPlayed = Number(localStorage.timesPlayed);
}
else{
timesPlayed = 0;
}
}
//code snippet to increase my variable
timesPlayed += 1;
//code snippet to set the localStorage
localStorage.timesPlayed = timesPlayed;
這很好用!?但從我讀過的,我應該使用localStorage.getItem和localStorage.setItem?
我應該改變我寫localStorage的方式嗎?
這僅僅是鏈接到網站,在那裏我學會了這種方式來訪問localStorage的 http://hacks.mozilla.org/2009/06/localstorage/
不,我知道,但從網上已經有的「外面」,我應該跟着別人做什麼? (指getItem,setItem) – Jacob 2011-06-15 00:23:52
這可能會有所幫助 - 關於setitem,getitem的好回答http://stackoverflow.com/questions/2010892/storing-objects-in-html5-localstorage – mrk 2011-06-15 00:28:45