Jump to content

ajax callback


etsted

Recommended Posts

A callback function is a function that is called upon the completion of another function. I would not consider it to be quite the same as an event handler, but maybe it can be.

 

http://stackoverflow.com/questions/2069763/difference-between-event-handlers-and-callbacks

 

Anyway in Ajax you make a request that is sent to the server and then you need to assign a function (whatever you want to call it) to handle the server response when it arrives...

xmlhttpreqobj.onreadystatechange = function(){  if (xmlhttpreqobj.readyState==4){     if(xmlhttpreqobj.status==200){        myprocessfunction( xmlhttpreqobj.responseText ); // process the response      }else{        // process the error     }  }}
Link to comment
Share on other sites

i was reading ajax in w3schools, and saw the example of callbacks functions, but i didnt get it. What does a callback function do. I tried searching a little but found no examples.

I doubt there are no examples of using callback functions. It is principal concept to Javascript.

https://www.google.com/search?q=using+callbacks+in+ajax&oq=using+callbacks+in+ajax&aqs=chrome..69i57.6078j0j7&sourceid=chrome&espv=2&es_sm=119&ie=UTF-8

 

Basically, it's a function (usually anonymous) that is used after the completion of some event, when you don't know when that event will complete. It's used with events and AJAX because you don't know long a request will take for example, but when it's complete, you would want to display a message to the user, or show something on the page.

 

jQuery uses this a lot with animation based features.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...