Retrieving information from next item in JSON (Twitter API)
I am trying to build a page that displays user tweets, however in between each tweet I want to visualise the space that represents what the user could have typed in that time period.
I have been going round and round in circles with the logic of what I am trying to achieve and getting nowhere. My current code is here: http://jsfiddle.net/k5234/, but this doesn't include the loop I am trying to achieve where the code takes the item'created_at field (date/time of current tweet) and takes it away from the next item.created_at field.
If anyone can point me in the right direction with how I can get my code working, I would be really grateful. i am working on the basis that 3x
s == 1 second.
To clarify, if a user tweets at 1.05pm and then again at 1.07pm, there would be 120 seconds of empty space ie. 120seconds * 3 characters = 360 blank spaces between the 2 tweets. Hope it all makes sense.
Thanks, David
ADDED INFO: Imagine someone continuously tweeting. The page will be full of text. However in reality, someone only tweets every so often. The space after the tweet (until their next one) will be of a size which represents what they could have typed in that time. Eg. I type a tweet, and don't tweet again for 10 minutes. Visu开发者_开发技巧alised on my page will be this last tweet, but the previous one will appear 1800 blank spaces after that (10 minutes * 60 seconds * 3 characters per second = 1800 blank spaces). What i can retrieve is the date of the tweet at time of need, but what I am having trouble with is getting the time of the next tweet to determine the gap at the stage of presenting the information in this manner. I can't really illustrate the concept any clearer than that.
I think your question is clear.
See if this solution works :-
function getTweets() {
html += "<pre>";
$.each(data, function(i, item) {
html += item.text; // add tweet
var timeCreated = new Date(item.created_at); // date of tweet
if( (i+1) in data)
{
var timeTillNextTweet = new Date( data[ i+1 ].created_at);
var timeDiff = timeTillNextTweet.getTime() - timeCreated.getTime();
var secondsDifference = Math.floor(timeDiff/1000);
// use secondsDifference to generate the spaces..
}
});
html += "</pre>";
}
hope this helps.
Maybe something along these lines:
var current;
var previous;
$.each(data, function(i, item) {
previous = current;
current = Date.parse(item.created_at);
html += "Tweet " + i +" " + (previous - current)/3000 + " spaces\n"
});
精彩评论