开发者

Ajax call to twitter and setting up an error or failure function

I have code on my site that uses ajax 开发者_运维知识库to pull in my twitter feed. It works excellent at the moment but my issue is that when the twitter call does not happen or if I am in a place that has twitter blocked via a firewall all I see is a blank area. I would love to have an error function that would let users know. "There was a error loading the twitter feed, Please trying again later."

Here is the code so far;

(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";
};

Any help would be great.

Thank you, James


Just set the $.ajax() attributes like this:

$.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 */ }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜