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

Tobias Graefe
Tobias Graefe
11,934 Points

Does the xhr object subscribe to the JSON file?

I've just finished the AJAX Basic course on the Front End track. If there are changes in the json file. (Updated by backend i.e. someone enters / leaves the office) Do I have to listen to those changes, or is the onreadystatechanged callback fired, if the data is updated?

2 Answers

There is a lot of information available as a starting point on this stack overflow

onreadystatechange is called when readyState changes and those states don't involve changes to the data itself. The states progress in order and once a readyState of 4 is reached the operation is complete.

There may be a better way than polling but from what I've read one means to refresh data is to use setInterval. I have a snapshot that demonstates. To use (1) preview the page (2) modify true/false values in either employees.json or rooms.json and save. The data is refreshed every 5 seconds.

Tobias Graefe
Tobias Graefe
11,934 Points

Thanks for your answer. Is polling a method that is used in the industry or does the Server tell the frontend, that there was a change? I tried to update the data in the sample and if I update the data it changed in the frontend without writing further code. Do you know why that is the case?