Alex Rivera | Logout

Callback function - use of parentheses

Asked 2011-07-05T22:19:32.270
18

I'm new to jQuery and am bit confused about the use (or not) of parentheses with a callback function. Say I have a function:

function cb() {
 // do something
}

Now what is the difference between:

$("p").hide(1000, cb);

and

$("p").hide(1000, cb());

Is it to do with when the cb function is executed? It would be great if someone could explain this to me in the simplest of terms.

Edit
Report

1 Answer

8

Is it to do with when the cb function is executed?

Essentially, yes, though the difference does run a little deeper than that.

  • cb is a reference of sorts to the function. You're passing the function along as a parameter to be invoked somewhere down the line.

  • cb() is a function call; the function will be invoked, and the result passed as an argument to .hide.

answered 2011-07-05T22:23:10.630

Your Answer