2016-11-13 56 views
0

我試圖在用戶完成遊戲循環或開始新遊戲時清除所有本地存儲,但也保留一些值。保持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行,所以我試圖將它打扮成我知道的最好的。

在此先感謝您的幫助或指導。

+0

你爲什麼要完全清除它,如果你想保留一些數據,而不是方式更多項目只是刪除你想要刪除的數據? –

+0

因爲我想清除其他所有東西,大約有20個左右的物品,我想要刪除,只有少數我想保留並傳遞到超出清晰點的遊戲狀態 – MrEhawk82

回答

0

我被困在這幾天,但我想我找到了一些可以工作的東西,所以我會回答我自己的問題,以防萬一後代有價值。

locatStorage.clear(); 
/* ^LS clear() function is above all new setItem codes, some variables are declared globally and some are declared at the top of the functional scope or as param^ */ 
var itemClass = document.querySelectorAll(".itemClass");//the strings are here 

if (itemClass) {//make sure some exist 
    for (var p = 0; p < itemClass.length; p++) {//count them 
     mdd = JSON.parse(itemClass[p].innerText);//parse the data for saving 

     localStorage.setItem("v", v);//this is the LS item that saves the amount of items i have, it's declared at the top of the functions timeline. 
     localStorage.setItem("LdrBrd_" + p, JSON.stringify(mdd));//this setItem function will repeat and increment with 'p' and assign the right string back to the key name it had before. 
    } 
} 

關鍵是保持字符串物理附加到一個元素,然後調用類名稱。我跑了一圈他們。 'mdd'會吐出我想要的每件物品。那麼剩下要做的就是將項目重新設置回原來的狀態。

這使我可以爲我的用戶創建一種方式,讓他/她在決定開始新遊戲時清除localStorage後收集獎盃並保留它們。

我使用CSS來隱藏字符串中的文本。

color:transparent; 

在我gameLoop,我有會讀取保存的字符串,並告訴他們剛剛隱藏的字符串下方卡的功能。

enter image description here

0

既然你想保留一些值,我建議兩兩件事之一:

  1. 不要叫localStorage.clear(),而僅消滅你想使用localStorage.removeItem('itemName')值。既然你說的項目名稱有一個數字組件,也許你可以做一個循環來減少代碼。

  2. 拉你想先保存的項目,並在撥打clear()後恢復它們。此選項是最好的,如果有,你想要移除,而不是保存(見下文)

function mostlyClear() { 
    var saveMe = {}; 
    saveMe['value1'] = localStorage.getItem('value1'); 
    saveMe['anotherValue'] = localStorage.getItem('anotherValue'); 
    localStorage.clear(); 
    for(var prop in saveMe) { 
     if(!saveMe.hasOwnProperty(prop)) continue; 

     localStorage.setItem(prop, saveMe[prop]); 
    } 
} 
相關問題