Problems when checking Local storage

0

Muy Buenas I have an inconvenience when trying to go through the local storage to assign classes and styles to elements in the DOM. The case is that an ajax request is sent that returns a load to the local storage I used setTimeout to wait for the localstorage to load, but in local it works without problems but already the thing is a little more delayed in test server, I want to know if it exists a js method that is activated or something similar to know if I can already consult the localstorage. this is what I do to consult it.

function addToCartProductInd(sku) {
    if (JSON.parse(localStorage.getItem('mage-cache-storage')) != null) {
        var Lstorage = JSON.parse(localStorage.getItem('mage-cache-storage'));
        if (Lstorage.cart && Lstorage.cart.items != null) {
            var Prod_Larecetta = Lstorage.cart.items;
            $.each( Prod_Larecetta, function( key, value ) {
                if(value.product_sku == sku){
                    $("#remove-itm-in-cart-"+sku).attr('data-item-to-remove',value.item_id);
                    $("#input-number-"+sku).val(value.qty).attr('data-item_id',value.item_id);
                }
            });
        }
    }
}
    
asked by Eric Js Martinez 28.09.2018 в 21:47
source

1 answer

0

You could read the localStorage in another function and assign the value to a global variable (x), then inside the function addToCartProductInd (sku) check if the value of the global variable x is different from null / false etc. If it's still null, you call back addToCartProductInd (sku).

    
answered by 29.09.2018 / 04:55
source