Software Archive
Read-only legacy content
17061 Discussions

getJSON with for looping

Muhammad_Y_Efendi
563 Views

 

I have to read several URL using JSON. I am using for looping to read each URL.

The problem is the getJSON result is always different base on URL respond.
Is it any way to wait for looping until getJSON process is finished ?

Thanks,

Muhammad

 

input_string = window.localStorage.getItem("all_id");
    var parts = input_string.split(',');

 for (var t=0; t<parts.length; t++){
        url = base_url + "products/" + parts + '/reviews?' + ck + '&' + cs;
  
        $.getJSON(url, function(data){
          $.each(data, function (index, value2) {
              review_detail = '';
            for (var i = 0, len = value2.length; i < len; i++) {
                star_number = get_star(value2.rating);
                //alert (star_number)
                review_detail += star_number + "<br>";
                review_detail += value2.review + "<br>";
                review_detail += value2.reviewer_name + "<br>";
                //alert(review_detail);
            }
          });    

        });

    
    }

 

0 Kudos
1 Reply
Elroy_A_Intel
Employee
563 Views

If you are trying to execute a specific action after every successful getJSON reponse, I recommend implementing a success function that calls the appropriate function wit the appropriate parameters. You won't need a for loop as you would be recursively calling your function that performs the necessary actions per the url.

You can learn more about the callback methods associated to getJSON at http://api.jquery.com/jquery.getjson/.

0 Kudos
Reply