html - Javascript syntax: Function insideof a For loop -
syntax issue here code works fine. have loop , inline function must run within (aquery callback).
(i=1;i <= 5;i++) { twitter[i] = $(this).find('twitter' + i).text(); //$('<div class="twitter[i]"></div>').html(twitter[i]).appendto('#link_'+i); $('.twitter[i]').html(twitter[i]).appendto('#link_'+i); // grab twitter $.getjson('http://api.twitter.com/1/users/show.json?screen_name='+twitter[i]+'&callback=?', function (data) { (j=1;j <= 5; j++) { twit_count[j] = data['followers_count'].tostring(); twit_count[j] = add_commas(twit_count[j]); $('#twitter_count'+j).html(twit_count[j]); } }); }
if i=3 want j same value within function.
the problem j loop runs 5 times every loop.
passing argument
function (data, i)
doesn't work, direction highly appreciated.
thanks,
not 100% sure want, if want have callback function 1 set of counters, shouldn't have loop in it.
to value of j inside callback have same value i, have write function return callback function, , pass desired value argument outer function.
$.getjson(url, ( function(j) { return function(data) { twit_count[j] = data['followers_count'].tostring(); twit_count[j] = add_commas(twit_count[j]); $('#twitter_count'+j).html(twit_count[j]); } } )(i) )
see answer here more explanation.
Comments
Post a Comment