Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2012-08-28
4
Medium Priority
?
917 Views
Last Modified: 2013-03-15
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
0
Comment
Question by:Ross Turner
  • 2
  • 2
4 Comments
 
LVL 61

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 38341117
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
 
LVL 7

Author Comment

by:Ross Turner
ID: 38341280
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
 
LVL 61

Expert Comment

by:Julian Hansen
ID: 38341542
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
 
LVL 7

Author Comment

by:Ross Turner
ID: 38342132
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month12 days, 11 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question