Random Positions / Fade in / Fade Out Jquery for Multiple Tweets

Hi EE,

I'm trying to make a basic tweet like cloud that  randomly positions the text which fades in and then fades out after 'xx' secs inside a div container.

I've got so far with creating one tweet to randomly appear but i'm stumped how to get the rest of them thru fading in / fading out and then back round on a loop.


<html>
<head>


<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
function rnd(max) { return Math.floor(Math.random()*(max+1)) }

$(document).ready(function() {
	
	
  
    // set your twitter id
    var user = 'stephenfry';
      
    // using jquery built in get json method with twitter api, return only one result
    $.getJSON('http://twitter.com/statuses/user_timeline.json?screen_name=' + user + '&count=20&callback=?', function(data)      {
          
        // Div Size Position 
		var txtmaxwidth = 800;
 		var txtimgwidth = 600;
		
		// result returned
		var tweet = "<div class='tweetitem' style='background:green;float:left;position:absolute;left:"+ rnd(txtmaxwidth - txtimgwidth) +"px;top:"+ rnd(txtmaxwidth - txtimgwidth) +"px' >" + data[0].text; + "</div>"
      
        // process links and reply
        tweet = tweet.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, function(url) {
            return '<a href="'+url+'">'+url+'</a>';
        }).replace(/B@([_a-z0-9]+)/ig, function(reply) {
            return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
        });
      
        // output the result
        $("#container").append(tweet);
        $(tweet).fadeIn();
	$(tweet).fadeOut(5000);

		
    }); 
      
});
</script>
</head>

<body>
<div id="container" style="width:800px; height:600px; position:relative"></div>
</div>
</body>
</html>

Open in new window


Many Thanks

Ross
LVL 7
Ross TurnerManagement Information Support AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Put your "Get Tweet" in a function and then use the setTimeout to call that function after you fade in

so (have not tested but will explain theory)
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
var tweet = '';  // No tweets yet
function rnd(max) { return Math.floor(Math.random()*(max+1)) }
$(document).ready(function() {
  get_tweet()
});
function get_tweet()
{
  if (tweet != '') $(tweet).fadeOut(); // If already shown tweet then fade it out.
  // set your twitter id
    var user = 'stephenfry';
      
    // using jquery built in get json method with twitter api, return only one result
    $.getJSON('http://twitter.com/statuses/user_timeline.json?screen_name=' + user + '&count=20&callback=?', function(data)      {
          
        // Div Size Position 
    var txtmaxwidth = 800;
    var txtimgwidth = 600;
    
    // result returned
    tweet = "<div class='tweetitem' style='background:green;float:left;position:absolute;left:"+ rnd(txtmaxwidth - txtimgwidth) +"px;top:"+ rnd(txtmaxwidth - txtimgwidth) +"px' >" + data[0].text; + "</div>"
      
        // process links and reply
        tweet = tweet.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, function(url) {
            return '<a href="'+url+'">'+url+'</a>';
        }).replace(/B@([_a-z0-9]+)/ig, function(reply) {
            return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
        });
      
        // output the result
        $("#container").append(tweet);
        $(tweet).fadeIn(); // Fade tweet in
        setTimeout('get_tweet', 5000);  // Sleep until next tweet to be retrieved
    } 
      
});
</script>
</head>

<body>
<div id="container" style="width:800px; height:600px; position:relative"></div>
</div>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ross TurnerManagement Information Support AnalystAuthor Commented:
Hi julianH,

I've just noticed a fatal flaw with my code,

   // using jquery built in get json method with twitter api, return only one result
   

Open in new window


Bugger!

Also i should have mentioned before... this is my first ever javascript / jquery  so i'm pretty much winging it....
0
Julian HansenCommented:
Can you maybe post what it is you are trying to achieve.

Do you want to cycle through a specific set of tweets i.e. download N tweets and cycle through those or continually refresh?
0
Ross TurnerManagement Information Support AnalystAuthor Commented:
Basically i'm trying to cycle thru the last 20 tweet starting with newest and make them fade in and out at random positions inside a div container.... trying to make a spooky esque tweet cloud if you like.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.