2013-01-23 33 views
1

我跑,我需要繼續運行,直到我得到迴應例如JavaScript回調函數調用本身,直到真正

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

} 

確定我遇到的問題是,它有時會給我空的響應函數我需要它再次運行。

所以我這樣稱呼它。

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?

感謝

+1

看看http://stackoverflow.com/questions/14483580/json-not-working-with-jquery #14483661 – salexch

+0

不幫助 – user1503606

回答

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(); 
     } 
    }); 
}); 
+0

謝謝沒有想到 – user1503606