Difference between event handlers and callbacks

前端 未结 12 2401
时光说笑
时光说笑 2020-12-22 16:33

What is the difference between an event handler and a callback function?

相关标签:
12条回答
  • 2020-12-22 16:57

    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.

    0 讨论(0)
  • 2020-12-22 17:00

    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 :

    • the developer defines a callback and passes it onto a library defined function (one which knows what to do with the callback so that the calling process or detecting process can call it) for e.g. in node,

    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 */}

    • during runtime, the detecting process receives the direct location of callback (because the library defined function did it for you) and calls it. This would mean 2 things:
      • the developers of the library would always need to know how to deal with different detecting processes as each may have a different way of calling back
      • and if the callback needs to be called multiple times, it might task the detecting process. For e.g. if the detecting process is a GUI process, then you would want the GUI thread to run with as much less task as possible, for a smooth GUI experience.

    Thus resulted the need for implementing a mechanism of callback, which solved these 2 issues:

    • an external process that would define a concurrent point for the library function to register callbacks and for the detecting process to notify as to when these registered callbacks need to be called.
      • This meant the developers of both these processes can now work independently of each other, without really knowing each other's ways.
      • This external process came to be known as event loop(in node, for e.g.). The term 'event' is simply the process of notifying the event loop by the detecting process and the registered callback came to be known as the event handler.
    • the external process(or the event loop) queued the events and executed them, thus taking the load off the detecting process which could now resume to whatever it was doing best.

    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.

    • The node js code above is a one-off event (as the connection to the server for a client is a one-off event while there can be many responses which are implemented as event handlers) and is an example of simple callback.
    0 讨论(0)
  • 2020-12-22 17:05

    Event handler is a callback from the system.

    0 讨论(0)
  • 2020-12-22 17:09

    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.

    0 讨论(0)
  • 2020-12-22 17:11

    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'
    
    0 讨论(0)
  • 2020-12-22 17:13

    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.

    0 讨论(0)
提交回复
热议问题