Information Technology Reference
In-Depth Information
currentRound = [];
$('ul, section').removeClass();
$('ul').empty();
getRandom();
//display the default twitter account icon
$('#tweet .avatar').html('<img src="' + defaultIcon + '" alt="" />');
//grab a random tweet from the correct user
$.getJSON(' https://twitter.com/status/user_timeline/' + correct +
'.json?count=10&callback=?', function(data) {
var ran = Math.floor(Math.random()*data.length);
console.log(data.length);
console.log(ran);
$('#tweet .content').html(data[ran].text);
// randomize the currentRound array so the correct user isnt always first
currentRound.sort(function() {return 0.5 - Math.random()});
// grab avatars and display usernames
buildQuiz();
});
}
In our next two functions, we will initiate the application by calling the function init().
This function resets all the variables to start a fresh new game, parses the JSONP
Twitter API feed for the randomly selected verified Twitter account, and then passes
everything off to be built and displayed to the user through the buildQuiz() function.
Once the document loads into the browser and the DOM (Document Object Model - a
cross-platform representation of all of the objects in an HTML document) is ready to be
manipulated, the code below (Listing 2-8) runs. In jQuery, you can run your code when
the document is ready-the ondocumentready event in JavaScript land-by using the
following shorthand $(function() { ... code goes here });
Once the document is ready, we will call the init() function, which will reset everything
on the page, grab a random user, and build the quiz as we went through before. We will
also assign the click event to all list items that will be written to the page so they end up
working like buttons. Once a user clicks a list item, the application will replace the
defaultIcon with the correct avatar from the user that posted the Tweet, update the
score if the user chooses correctly, and assign the gameover class to the game, which
will then tell the application to start a new round if the screen is tapped again.
Listing 2-8. Who's That Tweet? Javascript File, Part 4
$(function(){
// run the init() function and start the application
init();
// check for correct user on user selection
$('ul li').live('click', function() {
var name = $(this).attr('rel');
 
Search WWH ::




Custom Search