2015-09-27 85 views
0

我的应用获得了批准publish_actions。每个帖子之间的时间间隔

我想在feed文章发表评论。

一切工作正常。

以下是我的工作代码。

function home(token){ 
jQuery.ajax({ 
url:'https://graph.facebook.com/me/home?fields=id&limit=2&method=get&access_token='+token, 
dataType:'jsonp', 
success:function(data){ 
post_comment(data,token); 
} 
}); 
} 

function post_comment(list,token){ 
for(i=0;i<list.data.length;i++){ 
jQuery.ajax({ 
url:'https://graph.facebook.com/'+list.data[i].id+'/comments?message=testing&method=POST&access_token=' + token, 
    dataType:'script', 
success:function(){ 
gonderildi += 1; 
if(gonderildi >= list.data.length){ 
} 
} 
}); 
} 
} 

输出。

https://graph.facebook.com/XXXXXXXXXXXX/comments?message=testing&method=POST&access_token=XXXXXX. 
https://graph.facebook.com/XXXXXXXXXXXX/comments?message=testing&method=POST&access_token=XXXXXX. 

我只需要设置每个帖子之间的间隔。

https://graph.facebook.com/XXXXXXXXXXXX/comments?message=testing&method=POST&access_token=XXXXXX. 

Wait 5 sec. 

https://graph.facebook.com/XXXXXXXXXXXX/comments?message=testing&method=POST&access_token=XXXXXX. 

5秒后它应该发布的另一请求。 帮助。

+0

的setInterval? https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval – andrrs

+0

我尝试过,没有多少运气给我。 –

回答

0

您将需要使用闭包对于这一点,下面几行内容:

function post_one_comment(id, token) { 
     jQuery.ajax({ 
      url:'https://graph.facebook.com/'+id+'/comments?message=testing&method=POST&access_token=' + token, 
      dataType:'script', 
      success:function(){ 

      } 
     }); 
} 

function post_comment(list,token){ 
    for(i=0;i<list.data.length;i++){ 
     (function(_id){ 
      setTimeout(function(){ 
       post_one_comment(_id, token); 
      }, i * 5000); 
     })(list.data[i].id); 
    } 
} 
+0

先生它现在的作品。谢谢 –

0

的javascript:

function home(token) { 
 
    jQuery.ajax({ 
 
    url: 'https://graph.facebook.com/me/home?fields=id&limit=2&method=get&access_token=' + token, 
 
    dataType: 'jsonp', 
 
    success: function(data) { 
 
     post_comment(data, token); 
 
    } 
 
    }); 
 
} 
 

 
function post_comment(list, token) { 
 
    for (i = 0; i < list.data.length; i++) { 
 
    setTimeout(function() { 
 
     jQuery.ajax({ 
 
     url: 'https://graph.facebook.com/' + list.data[i].id + '/comments?message=testing&method=POST&access_token=' + token, 
 
     dataType: 'script', 
 
     success: function() { 
 
      gonderildi += 1; 
 
      if (gonderildi >= list.data.length) {} 
 
     } 
 
     }); 
 
    }, 5000); 
 
    } 
 
}

+0

示例代码?先生它的循环。 –

+0

无法正常工作。所有帖子在同一时间。我希望它在第一次后触发下一篇文章。 –

+0

这将不起作用,因为它只会创建list.ata.length数量的定时器,全部在5000ms之后触发,而不是在5000ms之后。另外,我的迭代器在所有定时器中都会得到相同的值。需要关闭。 – andrrs