Created
August 4, 2011 20:07
-
-
Save dalethedeveloper/1126105 to your computer and use it in GitHub Desktop.
Minimalist post-load of Twitter Statuses to a page using jQuery (with bonus relative time description + Wordpress wraps!)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function($) { | |
$.getJSON('http://twitter.com/statuses/user_timeline/Shitmydadsays.json?count=4&callback=?', function(data){ | |
var relative_time = function(datetime) { | |
var delta = parseInt((Date.now() - Date.parse(datetime)) / 1000, 10); | |
var r = ''; | |
if (delta < 60) { | |
r = delta + ' seconds ago'; | |
} else if (delta < 120) { | |
r = 'A minute ago'; | |
} else if (delta < (45 * 60)) { | |
r = (parseInt(delta / 60, 10)).toString() + ' minutes ago'; | |
} else if (delta < (2 * 60 * 60)) { | |
r = 'An hour ago'; | |
} else if (delta < (24 * 60 * 60)) { | |
r = '' + (parseInt(delta / 3600, 10)).toString() + ' hours ago'; | |
} else if (delta < (48 * 60 * 60)) { | |
r = '1 day ago'; | |
} else { | |
r = (parseInt(delta / 86400, 10)).toString() + ' days ago'; | |
} | |
return r; | |
} | |
var tw_statuses = ''; | |
$.each(data, function(i, tweet) { | |
if( tweet.text ) { | |
var tw = tweet.text.replace(/(https?:\/\/[^ ]+)/ig,'<a href="$1">$1</a>'); | |
// Lots of context specific HTML in here...but you get the gist. | |
tw_statuses += '<p>'+tw+'<br><a style="font-size: 85%;" href="http://twitter.com/Shitmydadsays/statuses/'+tweet.id_str+'">'+relative_time(tweet.created_at)+'</a></p>'; | |
} | |
}); | |
$('div.content.recent_tweets').eq(0).hide().append(tw_statuses).fadeIn(1000); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment