1

I am putting together a page displaying a user's tweets whilst visualising the amount of time in between in the way of blank spaces. Three blank space represent one second of inactivity between tweets. Confusing, I know. You can see from my code – http://jsfiddle.net/k5234/2/ – that I have written the individual  s into a for loop, but this is proving to be a very inefficient way of programming the page (as you can tell from the load speed!)

Does anyone have any suggestions as to how I can structure the JS better to make page loads quicker whilst keeping the same functionality.

Thanks, Dalogi

mVChr
  • 49,587
  • 11
  • 107
  • 104
sixtysticks
  • 786
  • 1
  • 11
  • 20
  • If you feel like doing it with ` `, you can try http://stackoverflow.com/questions/202605/repeat-string-javascript – Justin Peel Apr 26 '11 at 19:31
  • @Justin Peel I came across code like this recently, but it seems to run at a similar speed. I guess if I'm dealing with this amount of blank spaces, it's not gonna be much quicker unless I can somehow bloack the spaces together in larger chunks to begin with? – sixtysticks Apr 26 '11 at 21:17

6 Answers6

5

Sounds like a job for css padding/margins. No looping needed, just figure out how many px/ems it is for each second and you have a gap.

Eric

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • Tried CSS padding, but because I need every tweet to have an accurate number of spaces after the tweet, I have to put them all together within the `
    ` tag. Not sure how to add padding to each item within this element.
    – sixtysticks Apr 26 '11 at 20:54
  • 1
    wrap each twitter status text in its own element instead of using one pre. Set the padding on that element. – epascarello Apr 27 '11 at 16:13
0

Instead of blank spaces, use CSS margin/padding/whatever to space the items and calculate the relevant values based on the time between tweets. No more page bloat with  s

John Bledsoe
  • 17,142
  • 5
  • 42
  • 59
0

The best way would be to use CSS to pad-out the items. Just use tweetGap as a multiplier and use it to generate the number of pixels padding your require.

If, for some weird reason, you can do this and most of your cases are, say, less than 20 spaces then it would probably be more efficient to build an array and "index" into it using the tweetGap variable. Something like:

    var s=new Array();
    s[0]="";
    s[1]=" ";
    s[2]="  ";
    s[3]="   ";
    s[4]="    ";

    // etc

if (tweetGap < 20) {
  html+= s[tweetGap];
}
else {
 // forloop
}

Horrible code, but probably faster than a loop. But really CSS is the way to go.

Dan Diplo
  • 25,076
  • 4
  • 67
  • 89
  • Interesting, but if there are hours/days between tweets, this could be hundreds of thousands of spaces, and the array could get pretty big! – sixtysticks Apr 26 '11 at 21:06
  • Totally, it would only be efficient for short periods. But once you get into the realms of thousands of spaces then your whole concept of using space as a delimiter is called into question. – Dan Diplo Apr 27 '11 at 18:50
0

I agree with the other answers, but if you do need to use spaces:

function spaces(count) {
    var return_ = '';
    for(var i=0;i<count+1;i++) return_ += "&nbsp;";
    return return_;
}
document.write(spaces(10)); // ten spaces...

But really, padding/margin is the way to go:

/* CSS */
.spaces.five { margin-left: 3px; }
.spaces.ten { margin-left: 6px; }
[...]
<!-- HTML -->
blablablabla<span class='spaces five' />blablalabla
Tanner Ottinger
  • 2,970
  • 4
  • 22
  • 28
0

how about this

String.prototype.times = function(n) {
    return Array.prototype.join.call({length:n+1}, this);
};


"&nbsp".times(5);

WORKING DEMO

mcgrailm
  • 17,469
  • 22
  • 83
  • 129
0

This is a solution I created that works instantly.

http://jsfiddle.net/k5234/4/

$(document).ready(function(){

    $('#target').submit(function() {

        var html = "";
        var url = "http://api.twitter.com/1/statuses/user_timeline.json?&callback=?&screen_name=dalogi&count=5";    

        $.getJSON(url, function(data) {

            // Retrieving tweets
            function getTweets() {
        $.each(data, function(i, item) {
            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 = timeCreated.getTime() - timeTillNextTweet.getTime();
                var secondsDifference = Math.floor(timeDiff/1000);
                var tweetGap = secondsDifference * 3; // Based on 3 characters per second

                html += "<p style=\"margin-bottom: " + tweetGap + "em;\">" + item.text + "</p>";    
            } else { 
                html += "<p>" + item.text + "</p>";
            };
        });
            }

            getTweets();
            $('#userTweets').append(html);    
        });
        return false    
    });
});
ohmusama
  • 4,159
  • 5
  • 24
  • 44
  • Thanks for your answer. However it's the actual spaces (ie. inline ` `) after the tweet that I'm trying to reproduce, and not the number of lines below the tweet. – sixtysticks Apr 26 '11 at 21:04
  • What
    s? I need to create a series of blank spaces after each item to show a moment of inactivity on Twitter. Could be minutes, hours, days, etc, but the spaces within the stream are a visualisation of a break in tweeting.
    – sixtysticks Apr 26 '11 at 21:31
  • The solution that I gave you created the visualization of the "distance" that you needed, I just used css instead of `
    `s
    – ohmusama Apr 26 '11 at 22:31