Refreshing Server Data

Our Ajax application has fetched information from the server and then parsed the data and acted upon it. Now we’ll show you how to make the application retrieve a new version of the data from the server, which automatically refreshes the page. Script 13.9 contains the necessary JavaScript.

To refresh server information:

1.
function getPix() {
  xhr.open("GET", "flickrfeed.xml", true);
  xhr.onreadystatechange = showPictures;
  xhr.send(null);

  setTimeout(getPix,5 * 1000);
}
Where the previous script did the xhr call inside initAll(), this script pushes it down into its own function, getPix(). There’s one addition: the setTimeout() afterwards. Five seconds after the script has grabbed a random image, it goes and gets another. ...

Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.