Hi, I have a problem trying to go through the local storage to assign classes and styles to elements in the DOM. The thing is that an ajax request is sent that returns a load to the local storage. I have used setTimeout to wait for the localstorage to load, but in local it works without problems, but things take a little longer on the test server, I want to know if it exists a js method that is activated or something similar to know if I can already query the localstorage. This is what I do to query 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);
}
});
}
}
}
You could read the localStorage in another function and assign the value to a global variable (x), then inside the addToCartProductInd(sku) function check if the value of the global variable x is not null/false etc. If it is still null you call addToCartProductInd(sku) again.