Ajax调用twitter并设置错误或失败功能

时间:2011-02-17 16:18:23

标签: jquery ajax twitter

我的网站上的代码使用ajax来提取我的Twitter Feed。它目前运作良好但我的问题是,当Twitter呼叫没有发生或者我在一个通过防火墙阻止Twitter的地方时,我看到的是一个空白区域。我希望有一个让用户知道的错误功能。 “加载Twitter Feed时出错,请稍后再试。”

到目前为止,这是代码;

(function($) {
$.fn.twitscroller = function(options) {
    var
    defaults = {
        user: null,
        visible: 1,
        speed: 8000,
        vertical: true,
        count: 10
    },
    settings = $.extend({},
    defaults, options);

    this.each(function() {

        var $this = $(this);
        $this.html('');
        $this.addClass('twitscroller-replace');

        $.ajax({
            type: "GET",
            url: "http://twitter.com/status/user_timeline/" + settings.user + ".json?callback=?",
            data: {
                count: settings.count
            },
            dataType: "jsonp",

            success: function(res) {
                $('<ul>').appendTo('.twitscroller-replace')
                $(res).each(function(i, val) {
                    var title = val.text
                    .replace(/\b(https?|ftp|file):\/\/[\-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/ig, '<a href="$&">$&</a>')
                    .replace(/@(\w*)\b/ig, '@<a href="http://twitter.com/$1">$1</a>')
                    .replace(/#(\w*)\b/ig, '<a href="http://twitter.com/search?q=%23$1">#$1</a>');
                    time = TwitterDateConverter(val.created_at);
                    var link = "http://twitter.com/daspixl/statuses/" + val.id;
                    if (val.source == '<a href="http://bit.ly" rel="nofollow">bitly</a>') {
                        val.source = val.source.replace(val.source, '<a href="http://s.daspixl.com" rel="nofollow">http://s.daspixl.com</a>');
                    }
                    var source = val.source
                    $('<li></li>')
                    .html('<span class=\"title\">' + title + '</span>')
                    .append('<br /><span class=\"date\">' + time + '&nbsp; via &nbsp;' + source + '</span>')
                    .appendTo('.twitscroller-replace ul');
                });
                $('.twitscroller-replace').jCarouselLite({
                    vertical: settings.vertical,
                    visible: settings.visible,
                    auto: settings.speed
                });
                $('.twitscroller-replace').removeClass('twitscroller-replace');
            }
        });
    });
    return this;
}   
})(jQuery);

function TwitterDateConverter(time){
var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
    diff = (((new Date()).getTime() - date.getTime()) / 1000),
    day_diff = Math.floor(diff / 86400);

if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
    return;

return day_diff == 0 && (
        diff < 60 && "just now" ||
        diff < 120 && "1 minute ago" ||
        diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
        diff < 7200 && "1 hour ago" ||
        diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
    day_diff == 1 && "Yesterday" ||
    day_diff < 7 && day_diff + " days ago" ||
    day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
};

任何帮助都会很棒。

谢谢你, 詹姆斯

1 个答案:

答案 0 :(得分:0)

只需设置$ .ajax()属性,如下所示:

$.ajax({
    url:'',
    type:'GET',
    data: { key : value },
    dataType:'JSONP',
    success: function(){ /* The code you want to execute on a success call */},
    error: function(){ /* The code you want to execute on a fail call */ }
});