用于Facebook状态更新的Jquery计时器

时间:2013-09-16 10:13:33

标签: jquery facebook facebook-graph-api

我正在创建一个控件,从粉丝页面获取状态更新,然后我必须在标签中逐个显示它们。现在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.');
                        }
                    });
                });

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

1 个答案:

答案 0 :(得分: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);
    }
});