javascript callback function call itself until true

I am running a function that i need to keep running until i get a response example

exports.getJson = function(url, callback) {

    var loader = Titanium.Network.createHTTPClient();
    loader.open("GET", url);
    loader.onload = function() {
        var response = JSON.parse(this.responseText);
        callback(response);
    };
    loader.onerror = function(e) {
        callback(false);
    };
    // Send the HTTP request
    loader.send();

}

ok the problem i am having is it will sometimes give me a response of null and i need it to run again.

so i am calling it like this.

    url = 'http://example.com/test.json';
    main.getJson(url, function(response) {
            if(response){
                addData(response);

            }else{      
//return no response i need to run the function again now until it comes back as true
            }       
    });

Can anyone give me a good way to do this maybe try at least 3 times then return false???

Thanks

Answers


Just put the code in function and call it again:

var counter = 0;
function getData() {
    main.getJson('http://example.com/test.json', function(response) {
        if(response){
            addData(response);
        }
        else if (counter < 3) {
            counter++;
            getData();
        }   
    });
});

Need Your Help

Rails where In doesn't recognise where

ruby-on-rails ruby-on-rails-3.2 where-in

I keep getting an error undefined method where when running the code below with these parameters.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.