2012-01-02 30 views
0

有什麼方法可以從兩個不同的URL中總結出兩個不同的推特號碼?我的博客文章有多個分頁和用戶的鳴叫從不同勢的URL(在這種情況下,從第1頁第2頁或):如何使用jQuery將來自2個URL的推文計數加在一起?

例子:

HTTTP://mydomain.com/article/page/1 html的
HTTTP://mydomain.com/article/page/2.html

我目前使用jQuery和玩轉的水獺API一文顯示微博的數量。我想結合來自Page1和Page2的推文數量。

<script> 
    $(document).ready(function() { 
     url = "http://mydomain.com/article/page/1.html"; 

     // Get Number of Tweet Count From Topsy 
     $.getJSON('http://otter.topsy.com/stats.js?url='+url+'&callback=?', 
      function(data) { 
       $('#tweet').append(data.response.all); 
     }); 
    }) 
</script> 

回答

3

因爲查詢是異步的,則需要嵌套第二個查詢第一查詢

$(document).ready(function() { 
    url1 = "http://mydomain.com/article/page/1.html"; 
    url2 = "http://mydomain.com/article/page/2.html"; 

    $.getJSON('http://otter.topsy.com/stats.js?url='+url1+'&callback=?', 
     function(data) { 
      count1 = parseInt(data.response.all, 10); 
      $.getJSON('http://otter.topsy.com/stats.js?url='+url2+'&callback=?', function(data) { 
       count2=parseInt(data.response.all, 10); 
       $('#tweet').append(''+count1 + count2); 
      }); 
    });  
}); 
+0

這個工作的回調函數裏面!我只是將最後一行更改爲$('#tweet')。append(count1 + count2);謝謝! – Maca 2012-01-02 04:24:27

相關問題