简体   繁体   中英

When is localStorage cleared or data get deleted/removed?

I am working on implementing a shopping cart feature for online shopping web app using Spring Boot. I have used localStorage to store user's cart item inside it. But the thing is, suppose I have stored some items in the cart and closed my app and browser too and next day I opened that app again to see my cart item. But I did not find any item stored inside cart. So, is there any automatic removal of data when stored in browser using localStorage?

As, currently showing me zero items in cart. Is there like, when we exit or close the app or browser tab then data get removed automatically? Please suggest me good answers as I need to make my cart feature accurate. If need to share code then please let me know.

Below is my script to add items into cart:

function addtocart(productid, productname) {
            var category = document.getElementById('category').value;
            var productimage = document.getElementById("pimage").src; 
            var quantity = $("#quantity").val() != "" ? parseInt($("#quantity").val()) : 0;
            var price = $("#pprice").text();
            var pprice = price.replace(/[^\x00-\x7F]/g, "");
            var cart = localStorage.getItem("cart");
            var pcart = JSON.parse(cart) != null ? JSON.parse(cart) : [];
            var list = pcart;
            var present_or_not = pcart.findIndex(item => item.productid == productid);
            if (cart == null || present_or_not == null || present_or_not == -1) {
                    var product = {
                      productid: productid,
                      productname: productname,
                      pprice: pprice,
                      quantity: quantity,
                      productimage : productimage,
                      category : category,
                };
                pcart.push(product);
                localStorage.setItem("cart", JSON.stringify(pcart));
                swal({title: "Item Added", text: "Item added to cart", type: "success"},
                        function(){
                    location.reload();
                   })
              } else {
                var actual_stored_product = pcart[present_or_not];
                pcart.splice(present_or_not, 1);  
                var actual_qty = actual_stored_product.quantity == null || actual_stored_product.quantity == "" ? 0 : actual_stored_product.quantity;
                var actual_price = actual_stored_product.pprice == null || actual_stored_product.pprice == "" ? 0 : actual_stored_product.pprice;
                actual_stored_product.quantity = parseInt(actual_qty) + quantity ;
                actual_stored_product.pprice = parseFloat(actual_price)+parseFloat(pprice);
                pcart.push(actual_stored_product);
                localStorage.setItem("cart", JSON.stringify(pcart));
                swal({title: "Item Quantity Increased", text: "Item Quantity Updated of: "+productid, type: "success"},
                        function(){
                    location.reload();
                   })
              }
        } 

LocalStorage will not delete the data even if you close the browser and reopen

SessionStorage will clear out the data once the tab is closed

But its specific to browser. if you are running the project in different browser the data will not be present there

Refer this sandbox for example: https://codesandbox.io/s/n77o30n824

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM