【问题标题】:Displaying 30 Instagram Images of certain tag of certain userID显示特定用户 ID 的特定标签的 30 张 Instagram 图片
【发布时间】:2012-09-11 18:35:03
【问题描述】:

我有一个 Instagram API 调用请求带有 CrookedSpaces 标记的图像,当这些图像返回时,我正在过滤数据以确保只有来自某个用户的图像(使用他们的用户 ID),代码如下:

$(function() {
$.ajax({
    type: "GET",
    dataType: "jsonp",
    cache: false,
    url: "https://api.instagram.com/v1/tags/crookedspaces/media/recent/?count=100&access_token=TOKEN",
    success: function(data) {
        for (var i = 0; i < 31; i++) {
            var igUID = data.data[i].user.id;
            if(igUID === "USER ID") {
                $(".instagram").append("\
                    <div class='instagram-feed'>\
                        <img class='instagram-image' src='" + data.data[i].images.standard_resolution.url +"' width='325px' alt='" + data.data[i].user.id + " " + igUID + "' onMouseOver=\"toggle_visibility('igImageHover" + i + "');\"/>\
                            <div class='igHover' id='igImageHover" + i + "' onMouseOut=\"toggle_visibility('igImageHover" + i + "');\">\
                            <div class='igHover2'>\
                                SMALL TEST!\
                            </div />\
                            </div>\
                    </div>\
                ");
            } else {
                    console.log("Else portion of code ran " + elseCount + " time(s).");
                    ++elseCount;
                }
        }                
    }
});
});

但是,我只能显示 27 张图片,因为有 4 张图片不是由该特定用户 ID 发布的。有没有办法强制 for 循环不增加?或者在不将代码发送到无限循环的情况下从 i 中减去 1?

这里是 JSFiddle -- http://jsfiddle.net/UQcZP/

【问题讨论】:

  • 您能发一个JS Fiddle 以供我们合作吗?
  • 也许使用返回数组的大小作为你的 for 条件,并在用户 id 匹配时增加一个计数器,然后在你拥有所需数量的图像时退出循环

标签: javascript jquery api instagram


【解决方案1】:

你可以使用while循环:

var i = 0;
var used=0;
while (used<31)
{
  if (i < data.data.length){
     // .. Do your work here
     // only increment used when you actually get a match
  }
  else
  {
     // Here we are past the end of the data and did not reach the 30 items, 
     // so just set the number of items to 31 to exit the loop.
     used = 31; 
  }
  i++;    
}​

【讨论】:

  • 这个答案效果很好,但是我在编码方面走了一条不同的路线。想我至少可以投票/结束这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多