tholo tholo - 1 month ago 4
AngularJS Question

Get several JSON files in one function

The structure of JSON link is as follows:


www.something.com/link-number-rest-of-the-link.com.json


Now I need to get several JSON link files and the only thing that changes is the number part in the link above. Let's say it ranges from 10 to 40, so the first one looks like this:


www.something.com/link-10-rest-of-the-link.json


and the second one looks like this


www.something.com/link-11-rest-of-the-link.com.json


and so on until it reaches number 40.

Is there a way I can get it all in one function. I've tried this:

var nmr = function({for(nmr=10;nmr<40;nmr++)});

var json = 'www.something.com/link'+nmr+'rest-of-the-link.json';


but it won't work.

Is there a way to achieve this?

Please note that I have't put the "http" part as the SO automatically links it.

Thanks.

Answer

you can build array like this

var links = [];
for(var i=2005;i<2015.length;i++){
links.push('http://www.link.com/an-'+i+'-rest');
}
//now make your request for each link

another example

var requested = 0;
   function startLoading(){
     if(requested==2015)return alert("all files loaded");
     makeRequest('http://www.link.com/an-'+requested+'-rest');
   }
 function makeRequest(url){
     //the body of request
     //then
     //if response is ready make what you want for it and go next
     requested++;
     startLoading();
     // and startLoading(); to go to the next link
 }

startLoading();//start