Answer the question
In order to leave comments, you need to log in
What is a callback in programming?
What are callbacks?
What is their meaning and why are they needed?
Why are they called that?
I only know that this is a function that is passed as an argument to another function.
Answer the question
In order to leave comments, you need to log in
A callback is another function that will be called “later”. A household analogue is when you send an empty envelope
with your own address and stamps
in a letter to the radio station so that they send an answer in it.
This is usually necessary for asynchronous execution - when lines of code are not sequentially executed one after another, but some long task is started (and released) and it is not yet known when it will end.
For example, you need to make an HTTP request to a remote server and get a response from it.
Then, not only the request parameters are passed to the function that will deal with communication with the distant server, but also the callback function that will be called after receiving the response, with the data from the response.
When a function is running for a long time, there is no point in waiting for it to complete (visually it will look like a hung browser and the user will close the site). Therefore, a callback is passed to her, they say, you will inform yourself when you finish the work.
It's very simple:
You correctly said that this is a function that is passed as an argument to another function:
function first(callbak){
----здесь пишем код функции----
---- здесь можно записать код "функции обратного вызова"
и она сработает тогда, когда выполнится функция first()----
}
I applied in Direct Show (video processing). There is a third-party stream that passes frames through itself. The main program can constantly interrogate him if a new frame has appeared. But why waste time on surveys when they will only rarely return a positive result? Therefore, the main program simply passes a link to its handler (callback function), and the thread that skips the video will always call it at the right time (it is desirable that the handler has time to do everything quickly).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question