Search code examples
titaniumhttpclient

Set backbround of button with status of HTTPCLient Titanium


I have a button and when I click button,I will operate send a HTTPClient request..If success,i will change background image of button and set enabled= false ..If not success, don't change.How can I do? I used a variable and based on it but not success. This is my code of HTTP Client

var added;
var url ="http://192.168.1.14/add/";
button.addEventListenner('click',function(e){
   add(url);
   if(added==1)
   {
        //set background for button 
   }
   else
   {
      //don't set background
   }
});
function add(url)
{
var client = Ti.Network.createHTTPClient({
enableKeepAlive : true,
onload : function(e) {
  added=1;
  Ti.API.info('Load success');
  Ti.API.info('Status:'+this.status);
},
onerror : function(e) {
  added=0;
  alert(L('cant_connect'));
},
timeout : 10000 // in milliseconds
});

client.open("GET",url);
client.send();
}

Solution

  • change your button background in function add(url).

    onload : function(e) {added=1;Ti.API.info('Loadsuccess');Ti.API.info('Status:'+this.status);//set background for button },
    

    in case your callback returns an error the button background will remain same. This is because its asyn call. Once you call the function add(url) it will send the httpclient a request. The code next to this call will be executed and when the http call returns nothing will happened to you button's backgraound.