04.11.07
New Twitter badge
So, I just recently (about 3 hours ago) discovered the wonderful site Twitter.com. The gist of it is it allows you an easy (insanely so) way of letting people know what you’re doing. It’s pretty addictive. There are three things I especially like about this service.
- You have only 140 characters to type what you’re doing. This way not only do I not feel obligated to go on and on about what I’m doing, I’m actively forbidden to. Nice.
- You can send a text message from your phone to update what you’re doing. Perfect for when you’re out and about.
- You can set it up to remind you if you haven’t updated it in more than 24 hours (by text, natch). Should keep the updates from getting stale.
I searched around and found this post about how to get a list of the last 10 tweets on your website. Since it didn’t quite work for me, and I’m kinda anal when it comes to code, I fixed it up a bit. Essentially I fixed the bug in it that would cause it to fail if you have less than 10 tweets in your account and I removed the duplication of the count of tweets the javascript returns. Also, I added in a few extra “span”s for my css to make it all pretty.
You can find the code after the jump…
var elapsedTime = function(createdAt) {
var ageInSeconds = (new Date().getTime() - new Date(createdAt).getTime()) / 1000;
var s = function(n) { return n == 1 ? ‘’ : ’s’ };
if (ageInSeconds < 0) {
return ‘just now’;
}
if (ageInSeconds < 60) {
var n = ageInSeconds;
return n + ‘ second’ + s(n) + ‘ ago’;
}
if (ageInSeconds < 60 * 60) {
var n = Math.floor(ageInSeconds/60);
return n + ‘ minute’ + s(n) + ‘ ago’;
}
if (ageInSeconds < 60 * 60 * 24) {
var n = Math.floor(ageInSeconds/60/60);
return n + ‘ hour’ + s(n) + ‘ ago’;
}
if (ageInSeconds < 60 * 60 * 24 * 7) {
var n = Math.floor(ageInSeconds/60/60/24);
return n + ‘ day’ + s(n) + ‘ ago’;
}
if (ageInSeconds < 60 * 60 * 24 * 31) {
var n = Math.floor(ageInSeconds/60/60/24/7);
return n + ‘ week’ + s(n) + ‘ ago’;
}
if (ageInSeconds < 60 * 60 * 24 * 365) {
var n = Math.floor(ageInSeconds/60/60/24/31);
return n + ‘ month’ + s(n) + ‘ ago’;
}
var n = Math.floor(ageInSeconds/60/60/24/365);
return n + ‘ year’ + s(n) + ‘ ago’;
}
// Make date parseable in IE [Jon Aquino 2007-03-29]
function fixDate(d) {
var a = d.split(‘ ‘);
var year = a.pop();
return a.slice(0, 3).concat([year]).concat(a.slice(3)).join(‘ ‘);
}
function twitterCallback(obj) {
var html = ‘’;
for (var i = 0; i < obj.length; i++) {
html += ‘<li>’;
html += ‘<span class="my_twitter_status_time"><div>’;
html += elapsedTime(fixDate(obj[i].created_at)) + ‘ I was…’;
html += ‘</div></span>’;
html += ‘<span class="my_twitter_status"><div>’;
html += obj[i].text;
html += ‘</div></span>’;
html += ‘</li>’;
}
document.getElementById(‘twitter_list’).innerHTML = html;
}
</script>
<ul id="twitter_list"></ul>
<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/4219781.json?callback=twitterCallback&count=10"></script>
