我需要调整以下代码,对3个单独的页面进行多次请求(调整& page = 1,& page = 2,& page = 3),除了重复3次,我该怎样才能合并这个?
$.getJSON("http://www.behance.net/v2/users/" + user + "/projects?api_key=" + api + "&callback=?&page=1", function (data) {
var project_str = "";
for (i = 0; i < data.projects.length; i++) {
obj = {};
obj = data.projects[i];
project_str += '<a class="link" href="#' + obj.id + '"><img src="' + obj.covers['404'] + '" /></a>';
}
$('#behance_container div').append(project_str);
});
答案 0 :(得分:0)
类似的东西:
for(var i=1;i<=3;i++)
{
$.getJSON("http://www.behance.net/v2/users/" + user + "/projects?api_key=" + api + "&callback=?&page=" + i, function (data) {
var project_str = "";
for (i = 0; i < data.projects.length; i++) {
obj = {};
obj = data.projects[i];
project_str += '<a class="link" href="#' + obj.id + '"><img src="' + obj.covers['404'] + '" /></a>';
}
$('#behance_container div').append(project_str);
});
}
答案 1 :(得分:0)
$.each([1,2,3], function(i, numb) {
$.getJSON("http://www.behance.net/v2/users/"+user+"/projects?api_key="+api+"&callback=?&page="+numb, function (data) {
var project_str = "";
$.each(data.projects, function(i, obj) {
project_str += '<a class="link" href="#' + obj.id + '"><img src="' + obj.covers['404'] + '" /></a>';
});
$('#behance_container div').append(project_str);
});
});
答案 2 :(得分:0)
由于$.getJSON
调用是异步的,您可能希望在向用户显示之前先收集所有数据。否则,每次检索/附加到DOM时,您都会得到一点“闪存”。以下是未经测试的,但应该有效(如果没有,我希望你能帮助你朝着正确的方向前进)。
var nrOfPages = 3,
projects = [];
for(var i = 1; i <= nrOfPages; i++) {
retrieveData(i);
}
// Retrieve the data by the pageNr
function retrieveData(pageNr) {
$.getJSON("http://www.behance.net/v2/users/" + user + "/projects?api_key=" + api + "&callback=?&page=" + pageNr, function (data) {
for (i = 0; i < data.projects.length; i++) {
projects.push(data.projects[i]);
};
if(pageNr == nrOfPages) { // Check if all the pages are retrieved
processData();
}
});
};
// Process the retrieved data
function processData() {
var project_str = "";
for (i = 0; i < data.projects.length; i++) {
obj = {};
obj = data.projects[i];
project_str += '<a class="link" href="#' + obj.id + '"><img src="' + obj.covers['404'] + '" /></a>';
}
$('#behance_container div').append(project_str);
};