1

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

1

1 Answer 1

4

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();
        }   
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.