getJson + setTimeout problem
function test() {
var rqUrl = "http://search.twitter.com/search.json?q=#apple+OR+#ipad&rpp=100&callback=?"
callTwitterSearchApi(rqUrl);
}
function callTwitterSearchApi(tiwtterRequestUrl) {
debug("request to twitter : " + tiwtterRequestUrl);
// *** FIRST CALL WORKS GREAT... ***
$.getJSON(tiwtterRequestUrl, callTwitterSearchApi_callback);
}
function callTwitterSearchApi_callback(jsonPostsResults) {
debug("callback");
if (jsonPostsResults == null) {
debug("Why is jsonPostsResults null? If I copy paste the request inside a browser, I get something =(");
return;
}
if (jsonPostsResults.error != undefined && jsonPostsResults.error != "") {
debug("twitter api error");
}
var posts = new Array();
$(jsonPostsResults.results).each(function() {
posts.push(this);
});
debug("Number of posts : " + posts.length);
if (jsonPostsResults.next_page != undefined && jsonPostsResults.next_page.trim() != "") {
debug("calling next request in 5 sec...");
// *** WHEN COMMING BACK FROM THAT LINE, JSON RESULTS == NULL?! ****
setTimeout("callTwitterSearchApi("http://search.twitter.com/search.json" + jsonPostsResults.next_page + "")", 5000);
}
}
function debug(message) {
document.getElementById('debug').innerHTML = message + " " + document.getElementById('debug').innerHTML;
}
At the method callTwitterSearchApi_callback
, the
jsonPostsResults var is always null when it comes back from the setTimeout (at the end of the
callTwitterSearchApi_callback function). is that normal?
P.S. Sorry for my last post, it wasn't well formatted!