Difference between event handlers and callbacks

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

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

相关标签:
12条回答
  • 2020-12-22 17:13

    Another aspect of this is that events describe something that happened in the past, whereas a callback is often used while something is happening.

    When an event fires, you're being told that something has happened. When a callback is used, you're being asked to participate in something.

    A library or framework might issue events that let you know something has happened. A framework offers you points at which you can plug in code (perhaps as callbacks) so that you can take actively part in a process.

    Part of the problem is that event, callback refer to technical mcehanisms as well as more abstract processes.

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

    An event handler is a type of callback. It's called whenever an event occurs. The term is usually used in terms of user interfaces where events are things like moving the mouse, clicking something and so on.

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

    Events - Think of a Server (Employee) and Client (Boss).One Employee can have many Bosses. The Employee Raises the event, when he finishes the task, and the Bosses may decide to listen to the Employee event or not.The employee is the publisher and the bosses are subscriber.

    Callback - The Boss specifically asked the employee to do a task and at the end of task done, the Boss wants to be notified. The employee will make sure that when the task is done, he notifies only the Boss that requested, not necessary all the Bosses. The employee will not notify the Boss, if the partial job is done. It will be only after all the task is done.Only one boss requested the info, and employee only posted the reply to one boss.

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

    A callback is function(method) you pass as an argument to another function(method). The function(method) receiving the parameter can call it, or share it so some other function(method) in the system can call it.

    An event handler is a function(method) called when an event happens. It can be a callback.

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

    Generally speaking, a 'callback' is under the control of the detecting process. So you tell a GUI manager "call myaction when this button is pressed" and the GUI manager calls the action when the button is pressed.

    Event Handlers on the other hand operate at one step removed. The GUI manager is configured to send messages to an event handler. You tell an event manager that button pushes are handled by the myaction program. When the button is pushed the GUI manager puts a message on the event handler's queue and gets on with GUI managing. The event handler picks up the message from the queue, sees it's a button push, fires up the myaction program, and moves on to handling the next event. Usually the myaction program will run as an independent thread or even a separate process.

    While the "event handler" pattern is more complex it is much more robust and less likely to hang when an action fails. It also makes for a more responsive GUI.

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

    I love how all these answers differ from each other.

    I would conclude from this, is that from an a terminology point of view Events and Callbacks are interchangeable. What they mean in a specific programming language or framework and differ though, because any platform tends to pick their favourite terms.

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