< Back

Speed Up Your Website By Lazy Loading Twitter

What’s the similarity between Twitter and AT&T? …dropped calls.

I’ve always wanted to start a post with a gut-busting joke and boy did I ever deliver. Though, I do hate to compare Twitter and AT&T. One company is focused on innovation and bridging human networks while another rewards its customers with aging hardware and less than spectacular customer service. I’ll let you decide which is which.

Back on track and onto this post’s topic. My website often calls out via the Twitter API to grab the latest tweets on page load. I could be smart and implement the most basic of caching, but figured I would be tweeting so often that I needed a fresh call every time. And, as I have been finding out, sometimes the Twitter’s API is super response and sometimes it’s not — or times out. This was disastrous to the load times of my website (note to self, never hogtie yourself to third party if you don’t absolutely have too). So, I decided remedy the situation with a quick jQuery bandaid.

What do we need?

HTML

<div id="tweet-box">
	<!-- twitter loading icon -->
	<img src=/images/ajax-loader-twitter.gif" width="31" height="31" />
</div>

Simple as it gets for HTML. The interface has a target div with an animated loading icon waiting to be replaced.

Javascript

$(function() {
	$.get('/extra/twitter', function(data) {
		$('#tweet-box').empty().html(data);
	});
});

Via jQuery, the above code sends a get request to CodeIgniter’s extra controller and replaces the contents of the target div when a response is returned.

Controller

class Extra extends Controller {
	function __construct() {
		parent::Controller();
   	}

	function twitter() {
		// Call to Twitter API Library
		// NOTE: This call is to a custom wrapper I put around the class
		$data['html'] = Twitter::get_tweets(TWITTER_USER);
		
		$this->load->view('html', $data);
	}
}

The twitter action of Extra controller calls our Twitter API Library which requests my latest tweet and sends the parsed response to the UI.

There it is

Super duper easy way to lazy load a Twitter API call.

Connect with us

Thank You!

We really appreciate your interest in what we do.

We'll get back to you as soon as we can.