發現使用sessionStorage顯示最近查看的產品的腳本非常好。只有一個問題:每個產品都可以存儲並顯示多次。sessionStorage如果值已存在
這裏的jsfiddle http://jsfiddle.net/r77oozh8/
應該有一個更IF檢查,如果目前的產品已經在存儲,這樣的事情:
// check if product already in storage
if(!productExistsInStorage()) {
var currentCount = parseInt(sessionStorage.getItem("storedCount"));
// add to storage
sessionStorage.setItem(currentCount.toString(), JSON.stringify(productInfo()));
// update storedCount var by one
sessionStorage.setItem("storedCount", (currentCount + 1).toString());
}
我試圖使它,但沒有成功。
任何提示如何不保存產品數據,如果它已經在sessionStorage中設置?
因爲OP關心物品的內容。 ID不斷遞增。 – Scimonster
這個問題沒有意義。你是否試圖在會話存儲中增加一個計數變量?! –
我瞭解問題的方式,用戶可以將項目添加到他們的存儲。每次,它都會被賦予一個新的ID,即使實際的物品已經存在。因此,內容需要檢查,而不僅僅是數字。 – Scimonster