0

I need to add a callback to the $.ajax() success callback function. I am working in jQuery 1.3

success : ajaxCartFormSubmitted(), function ajaxCartFormSubmitted(e) { longRunningOperation(); if (e)... }); 

My function which should run after ajaxCartFormSubmitted() is:

function modalopen () { if ($('#ajax-cart-popup .full').length) { openmodal('ajax-cart-popup'); } else { return false; } }; 

I tried to add callback to ajaxCartFormSubmitted(e) like this ajaxCartFormSubmitted(e, callback) and use it like this success : ajaxCartFormSubmitted('modalopen()'), but I get no results, it seems that the function is not firing. Is the problem in first e argument? Thank you.

Update: I need to fire modalopen function after the ajaxCartFormSubmitted function. Ideally in the success callback.

3
  • 5
    Note that you need to pass the function reference, ie, success: ajaxCartFormSubmitted,. Secondly note that jQuery 1.3 is over 10 years out of date. You need to update it Commented May 28, 2019 at 15:07
  • the arguments of success function are predefined, check api.jquery.com/jquery.ajax Commented May 28, 2019 at 15:14
  • You could also return $.ajax(... and use .done() Commented May 28, 2019 at 15:30

1 Answer 1

1

There's a few issues here. Firstly you need to pass a function reference to success. By invoking the function immediately you're actually assigning its return value to success, hence nothing appears to happen. To fix that simply remove the parentheses from the function name:

success: ajaxCartFormSubmitted, 

Next, to have modalopen() execute after ajaxCartFormSubmitted() you need to call it at that time. Passing the name of the function as an argument alone is not enough. You need to expressly call the function. You can either do this in ajaxCartFormSubmitted():

function ajaxCartFormSubmitted(e) { // do something... modalopen(); }); 

Alternatively you can provide an anonymous function to success which calls both functions:

success: function() { ajaxCartFormSubmitted(); modalopen(); }, 

Finally, a couple of notes. You need to update jQuery as 1.3 is over 10 years out of date. 3.4 is the latest version. Also I'd be careful with your function names. You appear to have both openmodal() and modalopen(), and that kind of similarity is just asking for problems.

Edit

The ajaxCartFormSubmitted function contains another function which needs some time to process. That is why I need to call my function after ajaxCartFormSubmitted

In which case you need to implement the callback pattern, as $.ajax() does, yourself. You can make ajaxCartFormSubmitted() accept a function as an argument which will be executed after the async operation completes, like this:

// in $.ajax settings: success: function() { ajaxCartFormSubmitted(openmodal); } // function definition: function ajaxCartFormSubmitted(cb) { some.longRunningOperation({ oncomplete: function() { cb && cb(); // invoke the function you passed in here, openmodal() in this case } }); } 
Sign up to request clarification or add additional context in comments.

8 Comments

The ajaxCartFormSubmitted function contains another function which needs some time to process. That is why I need to call my function after ajaxCartFormSubmitted
In which case you need to implement the callback pattern yourself. I've updated the answer to give you a rough idea
than you, one more question why should not I use callback ajaxCartFormSubmitted(e, callback) like this?
Ok, so (cb) means callback? The function is written originally like this ajaxCartFormSubmitted(e) should I just remove the (e)?
That would depend entirely on what value you're expecting e to have. With regard to the code I wrote, I just named the argument cb, short for 'callback'. It can be named anything at all which is a valid JS identifier
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.