1
function addItemToCart (id_1, id_2, id_3, id_etc) {
  jQuery.ajax({

  });
}

function clearCart()
{
  jQuery.ajax({
  });
}

function buyButton()
{ 
    clearCart();
    setTimeout(function()
    {
        redirect to cart;
        addItemToCart(283746 , 1, 1, "Months", "1294");
    }, 500);
}

I'm trying to make it so that instead of using a setTimeout it waits until the clearCart() has finished loading through ajax and then it runs the functions inside the setTimeout and I can't seem to figure this out.. I tried doing a callback but I'd say that it isn't correct..

function FirstFunction(callBack)
{
    clearCart();

    if (callback)
    {
        callback();
    }       
}
function SecondFunction()
{
    buyButton();
}

FirstFunction(SecondFunction);
1
  • Run it in the .done handler Commented Aug 28, 2015 at 15:59

2 Answers 2

7

If you want to run some code when an Ajax response has arrived, put it in the done or success handler function. That is what those functions are for.

Don't guess how long the request is going to take.

Sign up to request clarification or add additional context in comments.

2 Comments

you can do it by adding .done(function(data){ /* your function*/ }) after your ajax call.
@ABr, I tried the following but even with the done or the success it did not work, pastebin.com/7yMahd0L I also tried including it in the ajax success but no luck.
4

Need to use done or success callback to avoid any random waiting time. like below:

function clearCart()
{
  return jQuery.ajax({
  });
}

function buyButton()
{ 
    clearCart().done(function () {
        addItemToCart(283746 , 1, 1, "Months", "1294");
    });
}

1 Comment

@iBrazilian2 In clearCart(), you need to return the jQuery.ajax().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.