title: Ajax beforesend, succes, complete, loading icon
body: <p>So this is my "addtocart" function.</p>
  
  <pre><code>$(document).on("addtocart", function(e, object) {
    e.preventDefault();
    var $object = $(object);
    var target = $(object).attr("action");
    var quantity = $(object).find(".qty").val();
    var productID = $(object).data("productid");
    $.ajax({
      url: target,
      data: {
        qty: quantity
      },
      dataType: 'json',
      beforeSend: function() {
        if ($object.find(".loading").length &lt; 1) {
          $object.append('&lt;div class="loading"&gt;&lt;span class="fa fa-refresh fa-spin fa-3x fa-fw"&gt;&lt;/span&gt;&lt;/div&gt;');
        } else {
          $("#item_" + productID).removeClass("added");
        }
        $("#item_" + productID).addClass("adding");
      },
      success: function(data) {
        if (data.r === "success") {
          var blocks = data.update_blocks;
          $.each(blocks, function(key, item) {
            var block = blocks[key];
            $(block.key).html(block.value.trim());
          });
          $("#jm-mycart").trigger("show");
          $("#item_" + productID).removeClass("adding").addClass("added");
          if ($("#item_" + productID + " td:first-child").find(".added").length &lt; 1) $("#item_" + productID + " td:first-child").append('&lt;div class="added"&gt;&lt;span class="fa fa-check"&gt;&lt;/span&gt;&lt;/div&gt;');
        } else {
          alert("Noget gik galt");
        }
      },
      complete: function() {
        $object.find(".loading").remove();
      }
    });
    return false;
  })
  </code></pre>
  
  <p>My problem is that whenever I add an item for the first time, the loading appears. But after that no. 2 odd things about this.</p>
  
  <p>First, it works just fine on product pages, it is only on category pages this happens.</p>
  
  <p>Second, When I don't try to remove the loading on completion, it works fine on category pages, but of course, gets stuck on product pages.</p>
  
  <p>I really can't seem to work my way out of this. Any help much appreciated.</p>