Monday, May 20, 2024
136
rated 0 times [  141] [ 5]  / answers: 1 / hits: 80781  / 13 Years ago, wed, march 30, 2011, 12:00:00

I'm currently writing JavaScript and confusing about callback. I've found it's not kind of built-in functions though...

I'm now reading O'Relly JavaScript 5th Edition and it shows a sample code something like below:



getText = function(url, callback) // How can I use this callback?
{
var request = new XMLHttpRequest();
request.onreadystatechange = function()
{
if (request.readyState == 4 && request.status == 200)
{
callback(request.responseText); // Another callback here
}
}
request.open('GET', url);
request.send();
}


Basically, I suppose I don't understand the general idea of callback though... Could someone write a sample code to take advantage of callback above?


More From » asynchronous

 Answers
39

Callbacks are pretty simple and nifty! Because of the nature of AJAX calls, you don't block execution of your script till your request is over (it would be synchronous then). A callback is simply a method designated to handle the response once it gets back to your method.



Since javascript methods are first class objects, you can pass them around like variables.



So in your example



getText = function(url, callback) // How can I use this callback?
{
var request = new XMLHttpRequest();
request.onreadystatechange = function()
{
if (request.readyState == 4 && request.status == 200)
{
callback(request.responseText); // Another callback here
}
};
request.open('GET', url);
request.send();
}

function mycallback(data) {
alert(data);
}

getText('somephpfile.php', mycallback); //passing mycallback as a method


If you do the above, it means you pass mycallback as a method that handles your response (callback).



EDIT



While the example here doesn't illustrate the proper benefit of a callback (you could simply put the alert in the onReadyStateChange function after all!), re usability is certainly a factor.



You have to keep in mind that the important thing here is that JS methods are first class objects. This means that you can pass them around like objects and attach them to all sorts of events. When events trigger, the methods attached to those events are called.



When you do request.onreadystatechange = function(){} you're just assigning that method to be called when the appropriate event fires.



So the cool thing here is that these methods can be reused. Say you have an error handling method that pops up an alert and populates some fields in the HTML page in the case of a 404 in the AJAX request.



If you couldn't assign callbacks or pass methods as parameters, you'd have to write the error handling code over and over again, but instead all you have to do is just assign it as a callback and all your error handling will be sorted in one go.





[#93014] Monday, March 28, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
randall

Total Points: 492
Total Questions: 99
Total Answers: 103

Location: Solomon Islands
Member since Fri, Oct 8, 2021
3 Years ago
;