javascript - how to count total number of ajax response data sets inside for loop? -
i calling ajax , output api response in textbox. want count total number of data sets received(counteri) , display each time click button. example if click button first time want alert display counteri=20 , next time click button display counteri=40 and... counteri=60.
currently code keeps showing 20 each time , not adding values. 1 tell me how fix this.thanks
<script> var maxnumid = null; var counteri= null; function callapi() { $.ajax({ type: "get", datatype: "jsonp", cache: false, url: "https://api.somesite.com/......"), success: function(data) { maxnumid = data.pagination.next_num_id; (var = 0; < 100; i++) { $(".galaxy").append("<div class='galaxy-placeholder'><a target='_blank' href='" + data.data[i].link +"'><img class='galaxy-image' src='" + ok.images.standard_resolution.url +"' /></a></div>"); document.myform.outputtext.value = document.myform.outputtext.value+data.data[i].images.ok.url+'\n' ; //alert('www!'+i); counteri=i; } } }); counteri=counteri+counteri; alert('counteri now: ' + counteri); } </script> <body> <br> <center> <div id="mydiv"></div> <div class="galaxy"></div> <button id="mango" onclick="callapi()">load more</button> </html>
edit: adding in start of success added total number of records ajax response
var num_records = object.keys(data.data).length; num_records2=num_records2+num_records; alert('number of records:'+ num_records2);
and
var num_records2 =null; // outside function
ajax async calls.
move alert after for. not outside success callback.
Comments
Post a Comment