Last active
December 15, 2015 19:19
-
-
Save jasdeepkhalsa/5310406 to your computer and use it in GitHub Desktop.
Get back the latest tweet from Twitter by Jasdeep Khalsa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div id="tw-text">Come and join us on Twitter!</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var tw_div = '#tw-text'; | |
| var tw_default_text = 'Come and join us on Twitter!'; | |
| var timeout = 5000; | |
| var weekday = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat']; | |
| function twErrorCallback(){ | |
| $(tw_div).html(tw_default_text); | |
| console.error('Twitter service failed'); | |
| } | |
| /** | |
| * Twitter Latest Tweet | |
| * Get back the latest tweet from the @Surfdome Twitter timeline, otherwise if the service cannot be accessed, | |
| * throw a console error and fallback to the default text | |
| * @requires jQuery 1.3.2+ | |
| * @author Jasdeep Khalsa | |
| */ | |
| var tw_username = 'username'; // Add your @username here (without the @) | |
| var tw_num_tweets = '1'; | |
| var tw_timeline_url = 'https://api.twitter.com/1/statuses/user_timeline/' + tw_username + '.json?count=' + tw_num_tweets; | |
| var tw_error_callback = setTimeout( twErrorCallback, timeout ); // Start the error timer | |
| $.ajax({ | |
| url : tw_timeline_url, | |
| crossDomain : true, | |
| xhrFields : { | |
| withCredentials : true | |
| }, | |
| dataType : 'jsonp', | |
| success : function(data, textStatus, jqXHR) { | |
| clearTimeout(tw_error_callback); // Cancel the error timer | |
| var text = data[0].text || tw_default_text; | |
| var date = new Date(data[0].created_at); | |
| if(date != 'Invalid Date' || undefined || ''){ | |
| var minutes = (date.getMinutes()<10?'0':'') + date.getMinutes(); // Make sure minutes show properly when < 10 | |
| var day = weekday[date.getDay()]; | |
| var html = '<i>'+day+' '+date.getHours()+':'+minutes+'</i>'+' - '+text+'<br/>'; | |
| $(tw_div).html(html); // Update the element | |
| } else{ | |
| var html = text; | |
| $(tw_div).html(html); | |
| } | |
| }, | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment