1

My "success" response seems to be firing too fast, so I've been forced to go from this...

  $('.hs_cart button').click(function(){
    $.get($(this).attr('url'), {
      success: function(){
        refresh_mini_cart();
      }
    });
  });

to this...

  $('.hs_cart button').click(function(){
    $.get($(this).attr('url'), {
      success: function(){
        setTimeout(function(){
          refresh_mini_cart();
        }, 5000);
      }
    });
  });

I've also tried the following but am receiving a "404 not found"...

  $('.hs_cart button').click(function(){
    $.get({
      url: $(this).attr('url'),
      success: function(){
        refresh_mini_cart();
      }
    });
  });

What am I doing wrong where I'm having to insert a setTimeout??

This is the Woocommerce function I am attempting to fire...

  function refresh_mini_cart(){
    $.ajax($fragment_refresh).done(function(response){
      if(response.cart_hash.length !== 0)
        return true;
    });
  }
Ryan Prentiss
  • 1,492
  • 2
  • 25
  • 46
  • possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Dave Apr 20 '15 at 20:50
  • I don't think your syntax for `$.get()` is correct. [Docs](https://api.jquery.com/jquery.get/) – Jack Apr 20 '15 at 20:59
  • @jack I've added another version of the code, but am receiving a 404 error. – Ryan Prentiss Apr 20 '15 at 21:10
  • 1
    your refresh_mini_cart() method shouldn't be getting called at the moment. – Kevin B Apr 20 '15 at 21:15
  • @dave The link doesn't answer my question as I'm already attempting to use the success callback. – Ryan Prentiss Apr 20 '15 at 21:22

1 Answers1

1

Let's simplify your $.get() a bit. Try the below code.

$('.hs_cart button').click(function(){
    $.get($(this).attr('url'), function() {
        refresh_mini_cart();
    });
});

Your issue was that your syntax for $.get() wasn't correct.

Jack
  • 8,851
  • 3
  • 21
  • 26