1

応答の例を取得するまで実行し続ける必要がある関数を実行しています

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();

}

私が抱えている問題は、時々nullの応答が返され、もう一度実行する必要があることです。

だから私はそれをこのように呼んでいます。

    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
            }       
    });

誰かがこれを行う良い方法を教えてもらえますか?少なくとも3回試してからfalseを返しますか???

ありがとう

4

1 に答える 1

4

コードを関数に入れて、もう一度呼び出します。

var counter = 0;
function getData() {
    main.getJson('http://example.com/test.json', function(response) {
        if(response){
            addData(response);
        }
        else if (counter < 3) {
            counter++;
            getData();
        }   
    });
});
于 2013-01-23T16:03:24.860 に答える