0

我正在创建一个从粉丝页面获取状态更新的控件,然后我必须在标签中一一显示它们。现在 Facebook 部分工作得很好,我只是遇到了计时器问题。我得到了一组状态更新,我想遍历数组并每 5 秒显示一次状态更新。这是我到目前为止所拥有的:

$(document).ready(function () {
                    $.ajax({
                        //
                        url: 'https://graph.facebook.com/3fifteen/feed?limit=5&access_token={my access token}',
                        dataType: 'json',
                        success: function (response) {
                            for (var i = 0; i < response.data.length; i++) {                                
                                var message = response.data[i].message;
                                var temp = new Date(response.data[i].updated_time);                                
                                if (message) {
                                    $.timer(function () {
                                        $('#lblMessage').text(message);
                                    }, 5000, true);                                        
                                }
                            }
                        },
                        error: function () {
                            // error handling here
                            alert('There was an error loading Facebook data.');
                        }
                    });
                });

任何帮助将不胜感激。谢谢

4

1 回答 1

1

应该做的伎俩:

$(document).ready(function () {
    $.ajax({
        //
        url: 'https://graph.facebook.com/3fifteen/feed?limit=5&access_token={my access token}',
        dataType: 'json',
        success: function (response) {
            for (var i = 0; i < response.data.length; i++) {
                var message = response.data[i].message;
                var temp = new Date(response.data[i].updated_time);
                if (message) {
                    displayMessageWithTimeout(message, i*5000);
                }
            }
        },
        error: function () {
            // error handling here
            alert('There was an error loading Facebook data.');
        }
    });

    function displayMessageWithTimeout(message, timer) {
        setTimeout(function () {
            $('#lblMessage').text(message);
        }, timer);
    }
});

编辑(带循环)

$(document).ready(function () {
    $.ajax({
        //
        url: 'https://graph.facebook.com/3fifteen/feed?limit=5&access_token={my access token}',
        dataType: 'json',
        success: function (response) {
            var messagesArray = new Array();
            for (var i = 0; i < response.data.length; i++) {
                var message = response.data[i].message;
                var temp = new Date(response.data[i].updated_time);
                if (message) {
                    messagesArray.push(message);
                }
            }
            loopThroughArray(messagesArray); // Display each message once
            setInterval(function () {
                loopThroughArray(messagesArray);
            }, messagesArray.length * 5000); // Loop
        },
        error: function () {
            // error handling here
            alert('There was an error loading Facebook data.');
        }
    });

    function loopThroughArray(messagesArray) {
        for (var i = 0; i < messagesArray.length; i++) {
            displayMessageWithTimeout(messagesArray[i], i * 5000);
        }
    }

    function displayMessageWithTimeout(message, timer) {
        setTimeout(function () {
            $('#lblMessage').text(message);
        }, timer);
    }
});
于 2013-09-16T10:22:09.400 回答