8

I have the following function:

function loadProjects(pID) {

    $.ajax({
        url: myURL,
        success: function (dataJS) {XXXXXXXXXXXXXXXX}
    });
}

I call this function like so loadProjects(1);

Issue is I want to be able to define a callBack function after success, and I'd like to include it when I do loadProjects(1, callback:{whatever js is included here gets called back after success})

How can I have a function accept a callback? How can I pass a callback to that function?

Thanks

2
  • Everyone thank you. But I want callback to be anything I want. I don't want the callback to be static? Commented Apr 27, 2011 at 0:16
  • Thanks again but all these are static callbacks, it doesn't allow me to define things like callbackfunc(22) Commented Apr 27, 2011 at 0:23

5 Answers 5

24
function loadProjects(pID, callbackFunction)
{
    $.ajax({
        url: myURL,
        success: function (dataJS)
        {
            if(typeof callbackFunction == 'function')
            {
                callbackFunction.call(this, dataJS);
            }
        }
    });
}

Usage:

loadProjects(pID, function(dataJS)
{
    // do stuff with your dataJS, bear in mind you can call the parameter any name.
});
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for correctly guessing that he wanted to pass parameters to the callback
This question deserves a -1 for poor wording.
My dad always taught me that the tricky part isn't figuring out the solution, but figuring out the problem.
3

Here's how you can modify your function so that it can accept a callback.

function loadProjects(pID, callback) {
    $.ajax({
        url: myURL,
        success: function (dataJS) {
          if ($.isFunction(callback)) {
            callback.call();
          }
        }
    });
}

Here's how you would use it.

function myCoolCallback() {
  alert("I am cool");
}  

loadProjects(123, myCoolCallback);

Or you can use it with an anonymous function like so.

loadProjects(123, function() {
  alert("I am cool");
});

Comments

1
function loadProjects(pID, callback) {

    $.ajax({
        url: myURL,
        success: function (dataJS) { if (callback) { callback(); } }
    });

}

Invoking something like this:

loadProjects(111, function() { alert('hello!'); });

1 Comment

but how can I pass params to callback?
1

You can pass a function to another function as if it were any other object. Check this out:

function loadProjects(pId, callback) {

  $.ajax({
    url: myUrl,
    success: function() {
      callback.call(); // <-- invokes your callback with no args
    }

}

You might also read up on the MDC documentation (function.call()).

Comments

-1
function loadProjects(pID, callback) {
  $.ajax({
    url: myURL,
    success: function (dataJS) {
      // your initial method declaration goes here
      ...

      // just call the callback method at the end of the success method
      callback();
    }

Comments

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.