Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AJAX Basics (retiring) Programming AJAX AJAX Callbacks

Ryan Hellerud
Ryan Hellerud
3,635 Points

call back function and changestate event question

so the video talks about setting a call back function and also claims every time there is a state change it will call said function, does it not matter where the function is placed or if the function comes after the xhr.send(). Does the browser simple update the statechange property every change and check the call back function each time?

2 Answers

Hugo Paz
Hugo Paz
15,622 Points

Hi Ryan,

Every time the readyState changes, the onreadystatechange event is triggered, calling the callback function.

Because onreadystatechange is an event, the code won't get invoked until the ready state changes, which will occur at some point in the future when the request is complete.

Ryan Hellerud
Ryan Hellerud
3,635 Points

interesting, im still wrapping my brain around js events and the fact that they seems like properties but are actually events and checked when that event triggers. It is an interesting programming point that I will need to understand.