Reputation: 64904
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
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>
Upvotes: 2
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