Skip to content

Instantly share code, notes, and snippets.

@djp424
Last active August 15, 2018 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djp424/a9d234a0d14853cd2e422749914447bb to your computer and use it in GitHub Desktop.
Save djp424/a9d234a0d14853cd2e422749914447bb to your computer and use it in GitHub Desktop.
Fetch and append JSON data from list of URLs (in order just in case some of the requests are large)
// Get's list of urls, grabs the title in json, and appends them to a selector of your choice
function requester(urls, selector) {
if(urls === undefined || urls.length === 0) {
return;
}
var request = new XMLHttpRequest();
request.open('GET', urls[0]);
request.responseType = 'json';
request.onload = function() {
if(request.readyState === 4 && request.status === 200) {
for(var i in request.response) {
var node = document.createElement('li');
var textnode = document.createTextNode(request.response[i]['title']['rendered']); // Based on temp.json
node.appendChild(textnode);
selector.appendChild(node);
}
// Take first item out and send another request.
urls.shift();
requester(urls, selector);
}
};
request.send();
}
var urls = ['temp.json', 'temp.json', 'temp.json', 'temp.json', 'temp.json'];
var resultsDiv = document.querySelector('#results');
requester(urls, resultsDiv);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment