JQuery.length () with added elements

I have a little problem with the .length () property. I am trying to count the number of spans in each div with one class. These divs are added earlier in the script. The funny thing is, if I warn the variable corresponding to the length, everything works fine, if I remove the warning, it is not. I've never seen this before, so any help would be appreciated :) Here is the code:

getTweets();
tweetCounter = function(){
    $('.entry').each(function(){
        var nTweets = $('.tweet', this).length;
        var infoPane = $('.infoPane', this);
        var tLink = '<a href="#" class="tLink" title="Tweets of the day">' + nTweets + ' Tweets that day</a>'; 
        infoPane.append(tLink);
        //alert(nTweets);   
    });


}
tweetCounter();

As I said, he correctly adds when I uncomment the warning. If you comment, 0 is displayed on every DIV ... Any ideas?

Here's the getTweets function:

getTweets = function(){
var url = "http://twitter.com/status/user_timeline/charleshaa.json?count=30&callback=?";
$.getJSON(url, function(data){
    $.each(data, function(i, item) {
        var fullTDate = item.created_at;
        var splitTDate = fullTDate.split(" ");
        var tMonth = splitTDate[1];
        if (tMonth == "Jan"){
            tMonth = "01"
        } else if(tMonth == "Feb"){
            tMonth = "02"
        } else if(tMonth == "Mar"){
            tMonth = "03"
        } else if(tMonth == "Apr"){
            tMonth = "04"
        } else if(tMonth == "May"){
            tMonth = "05"
        } else if(tMonth == "Jun"){
            tMonth = "06"
        } else if(tMonth == "Jul"){
            tMonth = "07"
        } else if(tMonth == "Aug"){
            tMonth = "08"
        } else if(tMonth == "Sep"){
            tMonth = "09"
        } else if(tMonth == "Oct"){
            tMonth = "10"
        } else if(tMonth == "Nov"){
            tMonth = "11"
        } else if(tMonth == "Dec"){
            tMonth = "12"
        }
        var tDay = splitTDate[2];
        var tYear = splitTDate[5];
        var tDate = tDay + '-' + tMonth + '-' + tYear;
        var tText = '<span class="tweet">' + item.text + '</span>';
        //alert(tDate);

        var destination = $('#date_'+ tDate +'');
        destination.append(tText);

    });
});

}
+3
source share
1 answer

. , , .tweet, (, ajax-?). , , .

, . , .

function get_tweets() {
    var requests = [ ];

    for(var i = 0; i < 5; i++) {
        requests.push( $.getJSON('/foo' + i + '.json', function(data) {
            // do something and create `.tweet` nodes
        }) );
    }

    return requests;
}

$.when.apply( null, get_tweets() ).done( count_tweets );

jQuery 1.5.2+. get_tweets() 5 Deferred . $.when() , promises.

1.3.2:

getTweets = function( callback ){
    var url = "http://twitter.com/status/user_timeline/charleshaa.json?count=30&callback=?";
    $.getJSON(url, function(data){
        $.each(data, function(i, item) {
            var fullTDate = item.created_at;
            var splitTDate = fullTDate.split(" ");
            // and so forth....
        });

        if( typeof callback === 'function' )
            callback();
    });
}

getTweets( tweetCounter );
+3

All Articles