JSon/Jquery request with a setTimeout always returns a "null" result? (for Twitter Search API)

Posted by supermogx on Stack Overflow See other posts from Stack Overflow or by supermogx
Published on 2010-03-18T03:22:00Z Indexed on 2010/03/18 3:31 UTC
Read the original article Hit count: 269

Filed under:
|
|
|

I make a call to the twitter API. 100 posts are retreived + a properties that tells me what the next page to call is. So I wait 5 sec. and call that next page, but the JSon results in the callback function is always null the second time... I think it's probably a JQuery problem...

Here's a complete sample HTML code :

<html>
<head>
<script type="text/javascript" src="./jquery-1.4.2.min.js"></script>
<script>
function test() {
    var rqUrl = "http://search.twitter.com/search.json?q=%23apple+OR+%23ipad&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 + "\n" + document.getElementById('debug').innerHTML;
}
</script>
</head>
<body>
<input type="button" onclick="test();" value="test" /><br />
<textarea id="debug" cols="80" rows="20"></textarea>
</body>
</html>

at line 18, at the second callback (back from the setTimeout), the parameter "jsonPostsResults" is always returned as null... I have no idea why. If I copy paste that 2nd request in a browser, it returns 100 results. Anybody had a problem like that with the Ajax JQuery functions when calling it with a setTimeout?

© Stack Overflow or respective owner

Related posts about JSON

Related posts about jQuery