titanium - Javascript: Check if function is TRUE -


i have function, should return true, when if condition inside cyklus met. (i tested condition, works)

createbtn.addeventlistener('click',function (e){  var ch = function check(){ var url = "http://hotel.010.sk/skyfit/read.php"; var json, poc, vypis; var i=0;  var xhr = ti.network.createhttpclient({     onload: function() {     json = json.parse(this.responsetext);           (i = 0; < json.poc.length; i++) {              prnt = json.poc[i];                if(win.xtra_id == prnt.id_cv && picker.getselectedrow(0).title == prnt.datum && prnt.capacity <= prnt.cnt ){                  return true;             }          };      }  });   xhr.open("get", url); xhr.send();               };  ... 

but when call function here check turns me true, unless if condition in check() not met!

if(ch){  alert('something');        } 

how fix function true when condition met?

thanks.

this expression

if (ch) { 

is true, because ch function , toboolean(function) === true.

however if changed if (ch()) {..} not work anyway, because inside of function perform asynchronous operation. function returns without waiting finish. in case should use callbacks or promise patters. simplest callback.

var ch = function check(callback) {      // ...      var xhr = ti.network.createhttpclient({         onload: function () {             json = json.parse(this.responsetext);             var status = false;             (i = 0; < json.poc.length; i++) {                 prnt = json.poc[i];                 if (win.xtra_id == prnt.id_cv && picker.getselectedrow(0).title == prnt.datum && prnt.capacity <= prnt.cnt) {                     status = true;                 }             };             callback(status);         }     });      // ... };  ch(function(status) {     if (status) {         alert('something')     } }); 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -