我使用循环对 Instagram API 进行了多次调用,但它产生了我无法理解的奇怪结果。
for(var i = 0; i < tags.length; i++){
document.write('search: ' + i + 'round');
document.write(tags[i] + ' ');
document.write('<hr />');
var url = 'https://api.instagram.com/v1/tags/'+ tags[i] +'/media/recent?&client_id=5a7b13571ced47418dd539e6fc97a67f&count='+count+'&callback=?';
$.getJSON(url, function(data) {
console.log(data.data.length);
if(data.data.length === 0){
//$('ul.images').append('<li>No results</li>');
} else {
$.each(data.data, function(index, value){
//console.log(value);
var imgUrl = value.images.low_resolution.url,
imgUser = value.user.username,
imgLink = value.link;
it.push(imgLink);
document.write(value.link + '<br />');
//$('ul.images').append('<li><a href="'+imgLink+'" target="_blank"><img src="'+imgUrl+'"/></a></li>');
});
}
document.write('---------------------' + '<br />');
});
}
当我搜索三个标签,猫,狗,猪,结果是:
search: 0roundcat
search: 1rounddog
search: 2roundpig
5 instagram image links
---------------------
5 instagram image links
---------------------
5 instagram image links
---------------------
所以看起来它首先循环三个document.write()
语句然后进入$.getJSON()
语句。
有什么想法吗?