对象数组只想显示2,然后settimeout显示接下来的两个,依此类推

时间:2014-01-29 10:26:53

标签: jquery ajax

可能非常简单,但我对我的Twitter帐户中有一系列对象的逻辑有点挣扎。实际上是10。我想创建一个循环,它一次只显示两条推文然后隐藏它们然后在对象内的淡入淡出...希望你们可以帮助..谢谢

 $.ajax({


    url: 'grabtweets.php',
            type: 'POST',
            dataType: 'json',
            data: request,
            success: function(data, textStatus, xhr) {

                if (data.httpstatus === 200) {

                    var text, name, img, avatarURL,
                        appendTo = $( JQTWEET.appendTo );

                    // append tweets into page
                    for (var i = 0, currentData; i < JQTWEET.numTweets; i++) {

                        currentData = data[ i ];

                        // Avatar

                        avatarURL = currentData.user.profile_image_url.replace("normal","bigger");


                        img = '';

                        // Username
                        url = 'http://twitter.com/' + currentData.user.screen_name + '/status/' + currentData.id_str;

                        // Handle Image

                        if( currentData.entities && currentData.entities.media && currentData.entities.media.length ) {
                            img = '<a href="' + url + '" target="_blank"><img src="' + $(JQTWEET.appendTo).entities['media'][0].media_url + '" /></a>';
                        }

                        // Replace Infomration in template structure

                        text = JQTWEET.template.replace('{TEXT}', JQTWEET.ify.clean( currentData.text ) )
                            .replace('{avatarURL}', avatarURL)
                            .replace('{USER}', currentData.user.screen_name)
                            .replace('{USERNAME}', currentData.user.screen_name)
                            .replace('{IMG}', img)
                            .replace('{AGO}', JQTWEET.timeAgo( currentData.created_at ) )
                            .replace('{URL}', url );

                        // Appends Tweet To Docuement
                        appendTo.append( text );
                    }

                }
            }
        });

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

var appendTo = $( JQTWEET.appendTo ),
    tweetsToShow = 2,
    delay = 5e3,
    i = 0;

(function displayTweets() {
    appendTo.empty();
    for (var j=0; j<tweetsToShow; ++j) {
        currentData = data[ i ];

        var avatarURL = currentData.user.profile_image_url.replace("normal","bigger"); // Avatar

        var url = 'http://twitter.com/' + currentData.user.screen_name + '/status/' + currentData.id_str; // Username

        var img = currentData.entities && currentData.entities.media && currentData.entities.media.length // Handle Image
            ? '<a href="' + url + '" target="_blank"><img src="' + $(JQTWEET.appendTo).entities['media'][0].media_url + '" /></a>'
            : '';

        var text = JQTWEET.template // Replace Infomration in template structure
            .replace('{TEXT}', JQTWEET.ify.clean( currentData.text ) )
            .replace('{avatarURL}', avatarURL)
            .replace('{USER}', currentData.user.screen_name)
            .replace('{USERNAME}', currentData.user.screen_name)
            .replace('{IMG}', img)
            .replace('{AGO}', JQTWEET.timeAgo( currentData.created_at ) )
            .replace('{URL}', url );

        appendTo.append( text ); // Appends Tweet To Docuement

        ++i;
        i %= JQTWEET.numTweets;
    }
    setTimeout(displayTweets, delay);
})();