admin管理员组文章数量:1323157
I'm trying to display the top 5 videos of a channel. However I'm struggling to limit it to just the five.
var playListURL = ';alt=json&callback=?';
var videoURL= '=';
$.getJSON(playListURL, function(data) {
var list_data="";
$.each(data.feed.entry, function(i, item) {
var feedTitle = item.title.$t;
var feedURL = item.link[1].href;
var fragments = feedURL.split("/");
var videoID = fragments[fragments.length - 2];
var url = videoURL + videoID;
var thumb = "/"+ videoID +"/default.jpg";
list_data += '<a href="'+ url +'" title="'+ feedTitle +'"><div class="thumbcrop" style="background-image:url('+thumb+');background-size:cover; background-position:center;"></div></a>';
});
$(list_data).appendTo(".videoClass");
});
How can I achieve this? I have tried (unsuccessfully) clauses such as while(i<6)
and if(i<6)
but neither of them display any results at all.
I'm trying to display the top 5 videos of a channel. However I'm struggling to limit it to just the five.
var playListURL = 'http://gdata.youtube./feeds/api/users/example/uploads?v=2&alt=json&callback=?';
var videoURL= 'http://www.youtube./watch?v=';
$.getJSON(playListURL, function(data) {
var list_data="";
$.each(data.feed.entry, function(i, item) {
var feedTitle = item.title.$t;
var feedURL = item.link[1].href;
var fragments = feedURL.split("/");
var videoID = fragments[fragments.length - 2];
var url = videoURL + videoID;
var thumb = "http://img.youtube./vi/"+ videoID +"/default.jpg";
list_data += '<a href="'+ url +'" title="'+ feedTitle +'"><div class="thumbcrop" style="background-image:url('+thumb+');background-size:cover; background-position:center;"></div></a>';
});
$(list_data).appendTo(".videoClass");
});
How can I achieve this? I have tried (unsuccessfully) clauses such as while(i<6)
and if(i<6)
but neither of them display any results at all.
-
Is there a reason why you are not using
max-results
as a parameter in yourplayListURL
? Is there something I am not catching? – Ilu Commented Jun 26, 2013 at 10:56
2 Answers
Reset to default 4In the url that you are requesting your results you can add an extra parameters &max-results=5
so you can restrict the number of your feeds.
so you will have
var playListURL = 'http://.../uploads?v=2&alt=json&max-results=5&callback=?';
More on youtube data api parameters
If you don't want to get all the results, don't use each, which does iterate through all the results in the first place. Simple as that. For limited and known number of steps use simple logic as:
for (var i = 0; i <= 4; i++) {
var item = data.feed.entry[i];
// your other stuff goes here
}
...or, as other state correctly, ommit your query by 5 in the first place and then use each
freely.
本文标签: javascriptLimit number of JSON resultsStack Overflow
版权声明:本文标题:javascript - Limit number of JSON results - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742141945a2422618.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论