我正在尝试从Facebook检索数据并将其显示在我的网页上。我正在使用setInterval函数来迭代图像循环,但循环运行正确,只有第一次尝试才能跳过大部分数据。 在我正在使用的代码下面,
function loop(){
//console.log("l"+l);
//i++;
//window.clearTimeout(timer);
if(posts!=i){
i++;
len=response.albums.data[i].photos.data.length;
console.log("photos count:"+len);
console.log("i:"+i);
var interval1=window.setInterval(function(){
$('#cf').hide();
$('#libtn').hide();
console.log("j:"+j)
image= document.createElement('img');
image.src= response.albums.data[i].photos.data[j].picture;
document.getElementById('imgs').innerHTML='<img src="'+image.src+'">';
$('#imgs').hide();
likes=response.albums.data[i].photos.data[j].likes.summary.total_count;
document.getElementById('likes').style.textAlign="center";
document.getElementById('likes').innerHTML=likes+"Likes";
$('#likes').hide();
response.albums.data[i].photos.data[j].comments.total_count;
length=response.albums.data[i].photos.data[j].comments.summary.total_count;
document.getElementById('name').innerHTML="";
document.getElementById('comment').innerHTML="";
document.getElementById('circle2').innerHTML="";
for(var k=0;k<length;k++){
var pro='/'+response.albums.data[i].photos.data[j].comments.data[k].from.id+'/picture';
FB.api(pro,'GET',{"type":"small"},
function(response) {
im=document.createElement('img');
im.src=response.data.url;
document.getElementById('circle2').innerHTML='<img src="'+im.src+'">'+"<br />";
}
);
cmt=response.albums.data[i].photos.data[j].comments.data[k].message;
name=response.albums.data[i].photos.data[j].comments.data[k].from.name;
document.getElementById('name').innerHTML=name+":";
$('#name').hide();
document.getElementById('comment').innerHTML=cmt;
$('#comment').hide();
}
$('#cf').hide().fadeIn(1000);
$('#circle2').hide().fadeIn(1000);
$('#name').hide().fadeIn(1000);
$('#comment').hide().fadeIn(1000);
$('#imgs').hide().fadeIn(1000);
$('#likes').hide().fadeIn(1000);
$('#libtn').hide().fadeIn(1000);
j++;
// m++;
count++;
console.log("count"+count+"len"+len);
// alert("came here");
// mydis();
if(count==len){
console.log("set interval");
j=0;
count=0;
window.clearInterval(interval1);
}
},5000);
calls--;
/*if(calls==0){
console.log("set interval1");
window.clearInterval(interval);
}*/
j=0;
count=0;
l=(response.albums.data[i].photos.data.length)*5000;
console.log("l:"+l);
window.setTimeout(loop,l);
}//,60000);
}
if(calls!=0){
console.log("here");
loop();}
,控制台输出为:
photos count:5
i:0
l:25000
j:0
count1len5
j:1
count2len5
j:2
count3len5
j:3
count4len5
j:4
count5len5
set interval
photos count:1
i:1
l:5000
j:0
count1len1
set interval
photos count:1
i:2
l:5000
j:0
count1len1
set interval
photos count:4
i:3
l:20000
j:0
count1len4
j:1
count2len4
j:2
count3len4
FB.login() called when user is already connected
here
photos count:5
i:0
l:25000
j:0
count1len5
photos count:1
i:1
l:5000
j:0
count1len1
set interval
正如你所看到的,Fb.Login()的第二次调用使得setinterval函数跳过了4个图像。任何人都可以帮我解决这个问题吗?提前谢谢