What is a callback function and when do we use one instead of a standard no parametrized functions?
-
Why do you call it like a "callback" is something different from any other function? – zerkms Jul 31 '14 at 03:24
-
What is a "*standard no parametrized function*"? – Bergi Jul 31 '14 at 03:45
-
a function that takes in no paramaters – Jul 31 '14 at 03:53
6 Answers
From Wikipedia:
A callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time. The invocation may be immediate as in a synAhronous callback or it might happen at later time, as in an asynchronous callback. In all cases, the intention is to specify a function or subroutine as an entity that is, depending on the language, more or less similar to a variable.
Basically a callback function is a function that you pass as a parameter to some other event or function. It allows the code to execute the callback-function at a time of its choosing with parameters of its choosing.
For example:
function my_callback(evt){alert("Button clicked!");}
$('#button').click(my_callback)
We pass the function my_callback
to the event code, which can then pass its own event object to the function when jQuery decides it is appropriate

- 2,754
- 1
- 21
- 34
callback functions are used when a piece of code needs to be executed later. You will see this most often in javascript when you are binding an event handler or returning from an ajax post. Basically you are passing a function as a parameter. That function will be executed when your event fires. You can see an example of this on the page below
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener
function modifyText() {
var t2 = document.getElementById("t2");
if (t2.firstChild.nodeValue == "three") {
t2.firstChild.nodeValue = "two";
} else {
t2.firstChild.nodeValue = "three";
}
}
var el = document.getElementById("outside");
el.addEventListener("click", modifyText, false);
In this example, modifyText
is a function that is being being passed to the addEventListener
function. When the outside
button is clicked, the code inside of modifyText
will execute

- 920
- 6
- 19
A callback
is something like you tell someone Call me back when you finish your job!
. It means you don't want to keep waiting there, but still need to do something when your friend(maybe a database access function) finish their job.

- 19,732
- 32
- 90
- 138
JavaScript statements are executed line by line. However, with effects, the next line of code can be run even though the effect is not finished. This can create errors.
For removing those errors we use call back functions
Call back function are those function which will be executed when the event fires up.This are used in many javascript function like addEventListener()
etc.It is also known as call back pattern.
Check this link to understand and learn about it

- 6,171
- 3
- 21
- 26
Code like this
var result = db.query('select * from T'); //use result - Standard function
your software is doing nothing and you are just waiting for the database to respond. this somehow either blocks the entire process or implies multiple execution stacks.
But a line of code like this
db.query('select * from T',function(result){
//use result - Callback function
});
allow the program to return to the event loop immediately.
In this execution, server makes that request and continue doing other things, when the request comes back(after millions of clock cycles), you can execute the callback, all you need is the pointer to the callback.

- 9,326
- 5
- 21
- 34
There is no distinction at a "language level" bertween a function
and a callback
. For what Javascript is concerned of they are the same thing, both are a function
.
The usage of a function makes the difference. If you invoke it in your code it's considered a function
, if it's passed as an argument to another function that will use it later on it's considered a callback
.

- 1,824
- 15
- 20