updating ajax cart

Highlighted
Excursionist
58 2 1

I have a countdown timer on my ajax cart and when there are no items in the cart I want to reset the timer. How would I in javascript get the update cart item amount so I can do this. It works fine with the normal cart but thats because the page is being reloaded how would I using javascript do the same on the ajax cart without force reloading?

  setInterval(function () {
    var cartContents = fetch('/cart.js')
    .then(response => response.json())
    .then(data => {
      if (data.item_count == 0){
     
        clearInterval(countdown);
        sessionStorage.removeItem('count');
      }
            console.log(data.item_count);
    });
  }, 1000);

The console.log picks up when the cart items are 0 but the timer doesn't reset or sessionstorage is not removed.

0 Likes