我試圖在用戶完成遊戲循環或開始新遊戲時清除所有本地存儲,但也保留一些值。保持localStorage對象迭代,同時在同一個函數中使用clear()
我已經可以用我的聲音值音量做到這一點:
// inside a conditional statement that fires when the user chooses to start a new game.
if (newGameBool === '1') {
var tst = myAu;
//myAu is the stored value that the user sets as sound using a range type input
localStorage.clear();
localStorage.setItem("Au", tst);//A newly cleared localStorage just got a new value, and it's the same as it was before.
UI.myLoad();//reload the function that uses LS to do things.
}
如何爲附加有一個迭代數字鍵的做到這一點?
這是我如何保存它們:
var i = +v + +1;
localStorage.setItem("v", i);
var vv = localStorage.getItem("v");
localStorage.setItem("LdrBrd_" + vv, JSON.stringify(LdrBrd));//saves all data with the iterating key name.
稱他們我做了聲音功能的方式:
var gv = v + 1;//v calls the value from LS and adjusted for off-by-one error. gv is a local variable.
if (newGameBool === '1') {
var ldd, vg;
for (var ii = 0; ii < gv; ii++) {
var ld = localStorage.getItem("LdrBrd_" + ii);
if (ld != null) {
//these are the values that i want to pass beyond the clear point
ldd = JSON.parse(ld);//JSON string of data saved
vg = ii;//how many of them.
}
}
localStorage.clear();
for (var xx = 0; xx < vg; xx++) {
var nld = localStorage.getItem("LdrBrd_" + xx);
if (nld != null) {
localStorage.setItem("LdrBrd_" + ii, JSON.stringify(ldd));
}
}
localStorage.setItem("v", vg);
UI.myLoad();
}
我一直在使用在各個景點的console.log(),看看有什麼正在進行。我評論清楚的功能只是爲了看看這些值是否是錯誤的,而不是全部保存。我試圖做一個小提琴,但本地存儲沒有在那裏工作。在Visual Studio中,它工作正常,但該文件的腳本長度差不多有2000行,所以我試圖將它打扮成我知道的最好的。
在此先感謝您的幫助或指導。
你爲什麼要完全清除它,如果你想保留一些數據,而不是方式更多項目只是刪除你想要刪除的數據? –
因爲我想清除其他所有東西,大約有20個左右的物品,我想要刪除,只有少數我想保留並傳遞到超出清晰點的遊戲狀態 – MrEhawk82