Thanks for taking the time to review my code. This function combines a twitter feed and an instagram feed and sorts the items by date. I just want to write better, more efficient code, so any tips on how to transform this into more of a "pro" javascript function would be much appreciated! Perhaps combining all these functions into one namespace?
The result of this code can be seen here: http://fwy.pagodabox.com/ (scroll down, it's in the right sidebar)
Thanks!
function extract_relative_time(date) {
var toInt = function(val) { return parseInt(val, 10); };
var relative_to = new Date();
var delta = toInt((relative_to.getTime() - date) / 1000);
if (delta < 1) delta = 0;
return {
days: toInt(delta / 86400),
hours: toInt(delta / 3600),
minutes: toInt(delta / 60),
seconds: toInt(delta)
};
}
function format_relative_time(time_ago) {
if ( time_ago.days > 2 ) return 'about ' + time_ago.days + ' days ago';
if ( time_ago.hours > 24 ) return 'about a day ago';
if ( time_ago.hours > 2 ) return 'about ' + time_ago.hours + ' hours ago';
if ( time_ago.minutes > 45 ) return 'about an hour ago';
if ( time_ago.minutes > 2 ) return 'about ' + time_ago.minutes + ' minutes ago';
if ( time_ago.seconds > 1 ) return 'about ' + time_ago.seconds + ' seconds ago';
return 'just now';
}
$j(document).ready(function($){
var fwyFeed = [],
feedIndex,
feedResponse,
feedHeight,
feedOutput = '',
user = 'friendswithyou',
$feedBox = $('.fwy-feed-content'),
$feedWrap = $('.fwy-feed'),
$feedLoad = $('.fwy-feed-load'),
$imgWrap,
$imgs,
imgLen;
$feedWrap.find('h2').html('Loading Feed... <span class="fwy-feed-load ic-icon fr f16">$</span>');
// Get instagram feed
$.ajax({
type: "GET",
dataType: "jsonp",
cache: false,
url: "https://api.instagram.com/v1/users/5774772/media/recent/?access_token=5774772.cf44785.bdf87268cff04f98b33581ad4ef60732",
success: function(data) {
for (var i = 0; i < 3; i++) {
var thedata = data.data[i],
created = thedata.created_time*1000,
url = thedata.images.low_resolution.url,
link = thedata.link;
fwyFeed[i] = {};
fwyFeed[i]["type"] = 'photo';
fwyFeed[i]["created"] = created;
fwyFeed[i]["text"] = url;
fwyFeed[i]["link"] = link;
fwyFeed[i]["date"] = format_relative_time(extract_relative_time(created));
}
}
});
// Wait 1.25 seconds for the instagram request to return
window.setTimeout(function() {
feedIndex = fwyFeed.length;
$.getJSON('http://twitter.com/statuses/user_timeline.json?screen_name=' + user + '&count=5&callback=?', function(data) {
for (i = 0; i < data.length; i++) {
var thedata = data[i],
created = data[i].created_at,
id = thedata.id_str,
tweetDex = i + 1 + feedIndex;
// Twitter time object given in human time, convert to timestamp
created = new Date(created);
created = created.getTime();
created = created.toString();
fwyFeed[tweetDex] = {};
fwyFeed[tweetDex]["type"] = 'tweet';
fwyFeed[tweetDex]["created"] = created;
fwyFeed[tweetDex]["text"] = thedata.text;
fwyFeed[tweetDex]["link"] = id;
fwyFeed[tweetDex]["date"] = format_relative_time(extract_relative_time(created));
}
});
// Wait 1.25 seconds for the twitter request to return
window.setTimeout(function() {
// Sort our feed array by time
fwyFeed.sort(function(a,b) {
return parseInt(b.created,10) - parseInt(a.created,10);
});
// Loop through each tweet/photo
for (var i = 0; i < fwyFeed.length; i++) {
if(i in fwyFeed) {
var type = fwyFeed[i]["type"],
created = fwyFeed[i]["created"],
text = fwyFeed[i]["text"],
link = fwyFeed[i]["link"],
date = fwyFeed[i]["date"];
if(type === 'photo') {
feedOutput += '<div class="bbd fwy-feed-item fwy-feed-photo"><a class="bgpale img-wrap pr ic-img block" target="_blank" href="' + link + '"><img class="pr img-loading center block" src="' + text + '" /></a><a href="' + link + '" class="cpink f11" target="_blank">' + date + '</a></div>';
} else {
feedOutput += '<div class="group bbd fwy-feed-item fwy-feed-tweet"><p class="f11">' + text + ' <a target="_blank" class="cpink" href="http://twitter.com/Friendswithyou/status/' + link + '">' + date + '</a></p>';
feedOutput += '<div class="fr fwy-feed-share tdn">';
feedOutput += '<a href="http://twitter.com/intent/tweet?in_reply_to=' + link + '" target="_blank" class="ic-reply"></a>';
feedOutput += '<a href="http://twitter.com/intent/retweet?tweet_id=' + link + '" target="_blank" class="ic-retweet pr"></a>';
feedOutput += '<a href="http://twitter.com/intent/favorite?tweet_id=' + link + '" target="_blank" class="ic-fav"></a>';
feedOutput += '</div>';
feedOutput += '</div>';
}
}
}
if(feedOutput){
$feedBox.append(feedOutput);
$imgs = $feedBox.find('img');
imgLen = $imgs.length;
$feedBox.addClass('show');
// Fade in images
if(imgLen > 0) {
$imgs.imgpreload({
each: function(){
$(this).removeClass('img-loading');
},
all: function() {
$feedWrap.addClass('loaded').find('h2').html('tweet/instagram');
$feedLoad.remove();
}
});
}
} else {
$feedWrap.addClass('loaded error').find('h2').html('Error loading feed');
}
}, 1250);
}, 1250);
});