2017-05-25 61 views
0

完成 - 編輯一旦JavaScript的:像櫃檯與記憶

我期待創造一個像反持續性內存

現在,我的項目存儲在USB驅動器上,而且我還沒有考慮將我的半成品網站上傳到互聯網。我揹着它,堵塞和工作。
該網站的一個功能,是心計數器像計數器,分別與他們的符號圖標。
我有一個小副本JavaScript文件,有十幾個函數來處理點擊事件等 - 如計數器的數量計數。

但是,隨着計數器的值是自動分配給臨時內存 - 如果你重新載入頁面 - 櫃檯數量將恢復到它的默認,零。一個巨大的頭痛...

從.txt

我想用實驗ReadFile()對象來處理這個問題的解讀 - 但我很快就發現,它需要用戶把文件(從我的考試)工作。
這裏是我的嘗試:

if (heartCount || likeCount >= 1) { 
    var reader = new FileReader(); 
    var readerResults = reader.readAsText(heartsAndLikes.txt); 
    //return readerResults 
    alert(readerResults); 
} 

加載時,該頁面通過規範操作運行,除了上述。
這,在我看來,將是理想的解決方案...

預防來自Cookies讀

餅乾現在看起來像一個選項不因爲它駐留在每臺計算機上。
它們存儲在計算機上的SSD,而不是在JavaScript文件...悲傷...

HTML5 Web存儲

使用新的Web存儲將是很大的幫助,大概。但同樣,它是在每臺計算機上,無論多麼美麗的系統...

localStorage.heartCount = 0 //Originally... 
function heartButtonClicked() { 
    if (localStorage.heartCount) { 
     localStorage.heartCount = Number(localStorage.heartCount) + 1 
    } 
    document.getElementById('heartCountDisplay').innerHTML = localStorage.heartCount 
} //Function is tied to the heartCountButton directly via the 'onclick' method 

不過,我質疑是否網絡存儲可以在USB驅動器結轉...

總結的想法

目前,我期待閱讀和編輯文件,因爲它對我的情況最爲理想。但是...
你會用哪一種?你會介紹一種新的方法嗎?
請告訴我吧! :)

+0

這是一個小型的網絡應用程序,你需要堅持一些計數器值嗎?你想要在用戶之間分享價值嗎? – Ben

+0

詞太多。你爲什麼要把它存儲在一個文件中?或者更準確地說,爲什麼你要將它存儲在一個文件中,而不是使用瀏覽器的持久性機制? –

+2

請將您的問題寫在最少的文字上,以解決您的問題。看起來您正在詢問一個非常廣泛的,可能是基於觀點的問題:「什麼是保存數據的好方法」。 –

回答

0
if (typeof(Storage) !== "undefined") { //make sure local storage is available 

if (!localStorage.heartCount) { //if heartCount is not set then set it to zero 
localStorage.heartCount = 0; 
} 

} else { 
alert('Local storage is not available'); 
} 

function heartButtonClicked() { 

if (localStorage.heartCount) { //if heartCount exists then increment it by one 
    localStorage.heartCount++; 
} 

//display the result 
document.getElementById('heartCountDisplay').innerHTML = localStorage.heartCount 
} 

這將只在每臺計算機的基礎上工作,並不會保留在您的拇指驅動器上。我能想到的將數據保存在驅動器上的唯一方法是手動下載JSON或文本文件。

+0

鑑於你是唯一願意回答的人(謝謝你),我會使用你的解決方案。它可能會或可能不會在我的項目中找到位置 - 但我會找到一種方法來合併它。再次感謝! – ghostwalker13

+0

歡迎您! – JoeBlow