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!

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 Stage 2 Challenge Answer

Can I use setinterval() to load that part after few seconds interval to make it more like live feed? Mine is not working

I like to use more like a live feed version. It is like when someone change the room availability to true from false in json file and without reloading the page that part will change from green to red automatically. I tried using setinterval() and call that ajax code as a anomanus function and set the interval time to 1000 but it didn't work. Can any tell me why it did not work? And what to do to make it work?

18,813 Points

Please post your code or a workspace snapshot. It's difficult to tell you what's wrong without seeing the problem.

1 Answer

Justin Coen
Justin Coen
Full Stack JavaScript Techdegree Student 12,829 Points

I also tried this. Initially I had incorrect syntax for my setInterval() which caused my code to error out. Once I fixed my syntax errors I realized that I needed to not only set an interval on my xhr.send() but my xhr.open() as well, because after the initial request is made the request closes.

After this it worked great, updating any changes made inside the employees.json file.

 setInterval(function() {
      xhr.open('GET', 'data/employees.json');
   }, 5000);