jquery - Ajax : Call multiple API with for loop -
since need call brunch of data make chart, need call 10 or more different api created function
function trend1() { return $.ajax({ url: '/dashboard/gettrend' + '?period=30d' + "&profileid=" + $(".numberofprofile0").html(), //getting api type: 'get', success: function(data) { } }); } function trend2() { return $.ajax({ url: '/dashboard/gettrend' + '?period=30d' + "&profileid=" + $(".numberofprofile1").html(), //getting api type: 'get', success: function(data) { } }); } function trend3() { return $.ajax({ url: '/dashboard/gettrend' + '?period=30d' + "&profileid=" + $(".numberofprofile2").html(), //getting api type: 'get', success: function(data) { } }); } function trend4() { return $.ajax({ url: '/dashboard/gettrend' + '?period=30d' + "&profileid=" + $(".numberofprofile3").html(), //getting api type: 'get', success: function(data) { } }); } function trend5() { return $.ajax({ url: '/dashboard/gettrend' + '?period=30d' + "&profileid=" + $(".numberofprofile4").html(), //getting api type: 'get', success: function(data) { } }); } function trend6() { return $.ajax({ url: '/dashboard/gettrend' + '?period=30d' + "&profileid=" + $(".numberofprofile5").html(), //getting api type: 'get', success: function(data) { } }); } function trend7() { return $.ajax({ url: '/dashboard/gettrend' + '?period=30d' + "&profileid=" + $(".numberofprofile6").html(), //getting api type: 'get', success: function(data) { } }); } function trend8() { return $.ajax({ url: '/dashboard/gettrend' + '?period=30d' + "&profileid=" + $(".numberofprofile7").html(), //getting api type: 'get', success: function(data) { } }); } function trend9() { return $.ajax({ url: '/dashboard/gettrend' + '?period=30d' + "&profileid=" + $(".numberofprofile8").html(), //getting api type: 'get', success: function(data) { } }); } function trend10() { return $.ajax({ url: '/dashboard/gettrend' + '?period=30d' + "&profileid=" + $(".numberofprofile9").html(), //getting api type: 'get', success: function(data) { } }); } $.when(trend1(), trend2(), trend3(), trend4(), trend5(), trend6(), trend7(), trend8(), trend9(), trend10()).done(function(trend1_data, trend2_data, trend3_data, trend4_data, trend5_data, trend6_data, trend7_data, trend8_data, trend9_data, trend10_data) { //do })
how may put massive code loop? ajax allow me this?
update 1:
function trend0()... function trend1()... function trend2()... function trend3()... .... $.when(trend1(), trend2()).done(function(trend1_data, trend2_data) { (var = 0; i<n; i++) // n n* of cycles eval("trend" + + "()"); }
}
am doing below?
solution :
var = ""; (var i=0; i<5; i++) { var trend = function(i) { if ( $(".numberofprofile"+i).length ) { return $.ajax({ url: '/dashboard/gettrend' + '?period=30d' + "&profileid=" + $(".numberofprofile"+ i).html(), //getting api type: 'get', success: function(data) { } }); } else { return false; } } i++ }
important : should not write functions since seem alike ! harder maintain in future
if know class number of profiles existing array, should indeed loop on it. if goes 0 10 above, may go way :
var responses = []; (var i=0; i<10; i++) { responses[i] = $.ajax({ url: '/dashboard/gettrend' + '?period=30d' + "&profileid=" + $(".numberofprofile" + i).html(), type: 'get', success: function(data) { } }
note actual response day found in responses[i]['response']
, or more responses[i]['responsejson']
edit: using $.when idea, seem have quite long set of ajax calls make cannot make final decisions since dont know final application. examples above :
var trend = function(profileno) { return $.ajax({ url: '/dashboard/gettrend' + '?period=30d' + "&profileid=" + $(".numberofprofile" + profileno).html(), type: 'get', success: function(data) { } } $.when(trend(0), trend(1), ..., trend(10)).done(function(t1, ..., t10) { //handle results here })
edit2 : surprised new solution works. have tried it? in spirit way :
var trend = function(profileno) { return $.ajax({ url: '/dashboard/gettrend' + '?period=30d' + "&profileid=" + $(".numberofprofile" + profileno).html(), type: 'get', success: function(data) { //you use callback assign retrieved "global" variable instance } }); } var i=0; var responses = []; while($(".numberofprofile"+i).length ) { responses[i] = trend(i); i++; }
Comments
Post a Comment