Adham
Adham

Reputation: 64904

How to set timer for retriving results for twitter search?

I am retrieving results from twitter using jQuery using the following code:

$(document).ready(function(){
var url='http://search.twitter.com/search.json?callback=?&q=';
var query="test";
        $.getJSON(url+query,function(json){
            $.each(json.results,function(i,tweet){
               $("#results").append('<p><img src="'+tweet.profile_image_url+'" widt="30" height="30" />'+tweet.text+'</p>');
        });
    });
});
}

But I want to retrieve new data every 5 seconds and append it to the div? How can I do that?

Upvotes: 0

Views: 143

Answers (2)

genesis
genesis

Reputation: 50982

<div id="results"></div>
<script>
function get(){
    var url='http://search.twitter.com/search.json?callback=?&q=';
    var query="test";
    $.getJSON(url+query,function(json){
          $.each(json.results,function(i,tweet){
            $("#results").append('<p><img src="'+tweet.profile_image_url+'" widt="30" height="30" />'+tweet.text+'</p>');
          });
    });
}
get();
setInterval(function(){
    get();
}, 5000);
</script>

demo

Upvotes: 2

jfriend00
jfriend00

Reputation: 707876

Use setTimeout from within your getJSON call and it's easier if you factor out the logic into a local function like this:

$(document).ready(function(){

    function getTwitterInfo() {
        var url='http://search.twitter.com/search.json?callback=?&q=';
        var query="test";
        $.getJSON(url+query,function(json){
            $.each(json.results,function(i,tweet){
               $("#results").append('<p><img src="'+tweet.profile_image_url+'" width="30" height="30" />'+tweet.text+'</p>');
            });
            setTimeout(getTwitterInfo, 5000);   // do it again in 5 seconds
        });
    }

    getTwitterInfo();   // call it the first time to get it started
});

FYI, you also have a mispelling of widt instead of width.

Upvotes: 1

Related Questions