Skip to content

Instantly share code, notes, and snippets.

@jasdeepkhalsa
Last active December 19, 2015 10:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdeepkhalsa/5939630 to your computer and use it in GitHub Desktop.
Save jasdeepkhalsa/5939630 to your computer and use it in GitHub Desktop.
Show tweets in the last hour in Tweet Counter (this is only the for loop, please see https://github.com/jasdeepkhalsa/tweet-counter/blob/master/tweet-counter.js for the full javascript file). By default brings backs tweets only in the last hour.
var today = new Date(); // By default brings backs tweets only in the last hour. Or just use new Date('Fri Jul 05 13:35:22 +0000 2013'), with a fixed cut off time, to have tweets within the specified time-frame only
// If you do not want the hourly window to change (otherwise "today" will become a moving target of tweets in the last hour each time tweet counter runs, then please put this line at the top of the file, inside the .ready function as follows:
// $(document).ready(function(){ var today = new Date(); })
// Making sure that the variable is defined outside of the var tweetCounter function
// Lets go through each tweet
for (obj in data.statuses) {
var tw_result = data.statuses[obj]; // This is the tweet
var tw_unique_id = tw_result.id_str; // This is the id of the tweet
var tw_created_at = tw_result.created_at; // This is the time of the tweet
var tweet_time = new Date(tw_created_at);
var num_of_hours = 1; // Tweets in the last hour.
var time_diff = (today - tweet_time)/1000/60/60; // Convert time from milliseconds into hours
// If the tweet occurs within the specified time-frame, count it
if(time_diff >= 0 && time_diff <= num_of_hours){
// Lets add this id into our array, only if it doesn't already exist
if ($.inArray(tw_unique_id, tw_tweets) === -1) {
tw_tweets.push(tw_unique_id);
};
}
// Okay we can update our page now
tw_count.html(tw_tweets.length);
tw_tweet.html(tw_username_open + tw_user_name + tw_elem_close + tw_text_delimiter + tw_text_open + tw_text + tw_elem_close);
// console.log(tw_result); // log each tweet object, for debugging
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment