Maca
Maca

Reputation: 1689

How to add together tweet count from 2 URL using jQuery?

Is there some way to sum up two different tweet number from two different url? My blog post have multiple divided pages and users tweet from diffrent urls (in this case from Page1 or Page2):

Example:

htttp://mydomain.com/article/page/1.html
htttp://mydomain.com/article/page/2.html

I am currently displaying number of tweets from one article using jQuery and Topsy's otter API. And I want to combine the number of tweets from Page1 and 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>

Upvotes: 0

Views: 338

Answers (1)

qiao
qiao

Reputation: 18219

since the query is asynchronous, you need to nest the second query inside the callback function of the first query

$(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);
            });
    });    
});

Upvotes: 3

Related Questions