Web Designer Pro Bundle - $500 of Site Templates, Stock Photos, Code, Graphics and more for only $20
How to Display Justin Bieber Tweets with Asynchronous Recursion
videos

How to Display Justin Bieber Tweets with Asynchronous Recursion

Tutorial Details
  • Topic: Justin Bieber News (or JavaScript)
  • Estimated Difficulty: Intermediate
  • Format: 20 Minute Screencast

Bottom line: Justin Bieber = traffic. I fully intend to use this to my advantage, and none of you can do anything about it. The purpose of today’s video tutorial is to demonstrate how to use a scary set of words, “asynchronous recursion” to continuously display updated tweets about the great Biebster. And then finally, we’ll hijack these tweets to make them look as if they’re referring to Nettuts+ instead.


The Full Source

<!DOCTYPE html>

<html lang="en">
<head>
	<meta charset="utf-8">
	<title>The Biebster</title>
</head>
<body>

	<h2> Latest Biebster Tweets </h2>
	<ul id="tweets"> </ul>

	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

	<script>

	(function() {
		var UpdatePanel = {
			init : function(options) {
				this.options = $.extend({
					interval : 5000,
					number : 3,
					hijackTweet : false
				}, options);

				this.updater();
			},

			updater : function() {
				(function updateBox() {
					this.timer = setTimeout(function() {
						updateIt();
						updateBox();
					}, UpdatePanel.options.interval);
				})();

				// get the ball rolling
				updateIt();

				function updateIt() {
					$.ajax({
						type : 'GET',
						url : UpdatePanel.options.url,
						dataType : 'jsonp',

						error : function() {},

						success : function(results) {
							var theTweets = '',
								 elem = UpdatePanel.options.elem.empty();

							$.each(results.results, function(index, tweet) {
								if ( UpdatePanel.options.hijackTweet ) {
									tweet.text = tweet.text.replace(/(Justin )?Bieber/ig, 'Nettuts');
								}

								if ( index === UpdatePanel.options.number ) {
									return false;
								}
								else {
									theTweets += '<li>' + tweet.text + '</li>';
								}
							});
							elem.append(theTweets);
						}
					});
				}
			},

			clearUpdater : function() {
				clearTimeout(this.timer);
			}
		};
		window.UpdatePanel = UpdatePanel;
	})();

	UpdatePanel.init({
		interval : 5000,
		number : 5,
		url : "http://search.twitter.com/search.json?q=bieber",
		elem : $('#tweets'),
		hijackTweet : true
	});

	</script>
</body>

</html>

Conclusion

Thanks for watching; I hope you enjoyed it! Stay tuned to Nettuts+ for more news and gossip on Justin Bieber!

Add Comment

Discussion 108 Comments

Comment Page 2 of 2 1 2
  1. Boba says:

    It’s funny how Justin Bieber provokes violence and hatred in comments.

  2. Brian Egan says:

    Another way to make the code even just a little shorter, for those who may be interested, is to utilize jQuery’s “jsonp” dataType. For example, you could remove this line:

    this.options.url += ‘&callback=?’;

    And change:

    $.ajax({
    type : ‘GET’,
    url : UpdatePanel.options.url,
    dataType : ‘json’,

    • Brian Egan says:

      Ack, accidental submission! Change the previously listed ajax to:

      And change:

      $.ajax({
      type : ‘GET’,
      url : UpdatePanel.options.url,
      dataType : ‘jsonp’, // jsonp instead of json

      The dataType: 'jsonp' will actually ‘add an extra “?callback=?” to the end of your URL to specify the callback.’ (from the jQuery API docs).

      I’m sure you were just being explicit about it for new-comers, but for those who want to be as lazy as I like to be, not a bad alternative.

      Cheers,
      Brian

  3. Ryan says:

    Don’t listen to all the stupid immature commenter’s who can’t take a joke Jeff; This was yet another quality tutorial.

  4. Tim says:

    When Justin sees this, she’ll be so pissed

  5. Sitebase says:

    Just what I needed for my new Justin Bieber fan site project.
    Thanks Jeffrey!

  6. Overkil says:

    Great tutorial, Jeff. I’m not sure how Bieber tweets are going to attract viewers, but eh, still amusing. I did, however, notice one small error in the above code, at line 52:

    var elem = UpdatePanel.options.elem.empty();
    //Should be
    var elem = UpdatePanel.options.elem;
    elem.empty();

    The code is right in the screencast, it just must have happened during repasting. Anyway, great concept with the tweets. I think it would great if you dissected jQuery’s ‘ajax’ method next (just sayin’ :) ).

  7. Great :P Love it!
    Did it get you more traffic to use Bieber in this tutorial?

  8. Simon says:

    Wading through all the idiots whinging about Justin Bieber, there are very few people actually commenting on the code.

    I would say that within an object such as UpdatePanel you should avoid referring to itself by variable name and instead use the this keyword.

    Makes it easier to update and maintain.

    You can easily set the context of the object methods with $.proxy, call or apply. Also the ajax method even has a context property to allow easy binding of this within it’s callback functions.

  9. Liam says:

    That background photo disturbed my will to watch the whole video, so i will figure it out myself how the code works great :)
    Please at least don’t put it on a background.

    emmm What about Lady GAGA initiative to drive more traffic in ?!

  10. w1sh says:

    I lol’d

  11. Changed the code so the new tweets load from the top and neatly scrolls down.

    Check it out: http://coley.co/demo/bieber.html

  12. To understand the code aspect of this tutorial, I think I must purchase a membedrship.

  13. Vladimir says:

    Useful tutorial, as usual, thank you!

    I’ve been changing slightly the base code, here is the the result app: http://www.myastrodata.com/dev/tweets.html

  14. cloud says:

    awesome, I need all the traffic I can get

  15. Probably Justin Bieber wasn’t already born when i’m started to navigate on internet :))

  16. Zachary says:

    What happened to the video? Copyright infringement?

  17. Giuliano says:

    As always brilliant job Jeffrey, I really enjoyed it.

    I’m a little bit surprised with so many Bieber comments. Do you guys get a joke? What’s the problem with you: No sense of humor, no girlfriend or both?

  18. Gregi says:

    Thanx Jeffrey, i think i’ll finally learn ajax thanks to this tutorial,

    Peace

  19. Shawn Tysco says:

    Here is a quick addition to the code – able to search results.

    http://tweetingwithtwitter.com/home.php

  20. PDesign says:

    You are pulling all tweets from twitter and just show 3 of them?
    so why don’t you just pull only 3 latest tweets?

    like

    http://search.twitter.com/search.json?q=bieber&rpp=3

    hope i am not wrong (:

    • Jeffrey Way says:
      Author

      No, you’re right. The only reason is to give the downloaders the option of setting how many to display. :) But sure, that’s a small update that could made to the init call.

  21. noname says:

    Dear God,

    Please, send Eazy-E back to us and we will bring you Justin Beaver.

    Signed – The Whole World.

  22. Antor says:

    Maybe I should do this on my blog. LOL!! Id think Nettuts has enough traffic already. Nice tutorial though.

  23. Fabryz says:

    Nice tutorial Jeffrey, i’ve just added a simple form to change the parameters for the query, and styled a bit =)

    http://labs.fabryz.com/twitter-asynch-recursion/

  24. isuez says:

    very nice thanks….

  25. bieberftl says:

    it’s sad how marketing works nowadays. It’s changed so much that pathetic, talentless hacks like Bieber can succeed. As long as corporate fatheads make money they could care less. I refuse to promote anything of the sort on my own sites.

  26. Brannon says:

    Are you going to write a book on JavaScript? I think I’d buy a copy!

  27. Nobody says:

    Who the heck is Justin Beinber?

    • SKyintheSeA says:

      go to youtube.com and type that name, then thumb up when you see something like ” if you’re here to see the page view, thumb up”

  28. iamjem says:

    I could be wrong, but it looks like there’s one error in your code.

    The UpdatePanel.updater method has the anonymous self executing function at the beginning which starts like:

    this.timer = …

    “this” will reference the window, and not the UpdatePanel object. So your clearTimeout call later, if used, wouldn’t do anything.

    Otherwise nice tutorial.

  29. Ben Howdle says:

    Hi, this is a great tutorial and very topical, love the Twitter integration. Have a read and download of this and see what you think: http://blog.twostepmedia.co.uk/live-twitter-trends-on-a-website/

    Thanks Nettuts, keep writing!

  30. sweet, interesting blog post! i find out so how to do justin bieber follow u of twitter
    its right here
    http://www.planetnana.co.il/punka1/justin-follow.htm

  31. Benedikt says:

    This is so awesome. Finally i can offer my clients full Bieber-Integration!

Comment Page 2 of 2 1 2

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.