What is the difference between an event handler and a callback function?
Callback (from Wikipedia): "executable code that is passed as an argument to other code".
Event handler (again from Wikipedia): "asynchronous callback subroutine that handles inputs received in a program".
Which happens to be the way I've always understood it: an event handler is a very specific type of callback.
James Anderson's answer is the most detailed. Expanding on his answer; Callback refers to any code, that is passed as an argument to a method, intended to be called later asynchronously. However, a callback doesnt define how the process of callback itself should be implemented. This is where the classification of callbacks begin. Traditionally, a callback process would look :
var server = require('http').createServer(function(req, res){/* your code */});
the createServer
is the library defined function which makes sure the detecting process gets to call the correct callback, which in this case is function(req, res){/* your code */}
Thus resulted the need for implementing a mechanism of callback, which solved these 2 issues:
Thus for events that occurred multiple times, the event loop or the event handlers became the way of implementing callbacks, while the original callbacks are still preferred for one-off events as you are not really tasking the detecting process and dont need to have the event loop for just one event as its not efficient.
Event handler is a callback from the system.
This question is very old but I found this link from MSDN very interesting. I hope anyone else that stumbles on this question gets something out of this link.
The underlying mechanisms are similar, but the semantics are different. Both callbacks and event handlers are called asynchronously.
The callback function is generally passed explicitly from a caller routine to request some information. The information is returned some time later, passed as arguments back to the callback by the callee. At this time, the calling routine completes its business. Often the callback is a closure - syntactically inside the calling routine, and often un-named (anonymous). It might look a bit like the below, in javascript:
function caller() {
someLibrary.getMeSomething(arg1, arg2, function(returnedData) {
// this is the callback which will do something with returnedData
});
}
So the callee (someLibrary.getMeSomething) is given an anonymous callback function, and some time later this function is called with the returnedData. A callback is like a single-shot event to a single receiver.
Events handlers are also 'called back', but generally they are used over an extended period for multiple events, like mouse clicks, network events etc. Also, multiple objects may be interested in the same event. For these reasons, you generally 'subscribe' or 'register' to events in setup code (like object initialisation), and the event handler is more typically a named method. Usually too, each event type is identified as a constant or string.
So in Python it might look like:
class MyUIClass:
def __init__(self):
someUILib.register(someUILib.events.MOUSE_CLICK, self.my_mouse_click_handler);
def my_mouse_click_handler(self, eventInfo):
# do something with event
if eventInfo.x < 100:
print 'You clicked in the margin'
A callback is procedure you pass as an argument to another procedure. The procedure receiving the parameter can call it, or share it so some other procedures in the system can call it.
An event handler is a procedure called when an event happens. It can be a callback.