AJAX Handling Poor Connections (timeouts and cancelation)

I have a DHTMLx Touch application wrapped in PhoneGap that I am trying to improve online/offline experience. To do this I need to monitor the Ajax connections and implement graceful handling of timeouts.

I cannot see any options in the API that provide a way to handle timeouts or cancellations, am I missing something?

Thanks,
James

Hello,

dhx.ajax() method returns XMLHttpRequest object. So, you can apply timeout property or about() method for it.

[code]
var callback = {
success: function(text,xml,obj){
//code here
},
error: function(text,xml,obj){
//code here
}
};

var xhr = dhx.ajax( url, callback );
xhr.timeout = 200;
if(…)
xhr.abort();[/code]

Can you also tell me if there is a way to cancel the dhx.delay function?

Thanks,
James

Oo, this is interesting. I have this problem also, AJAX calls taking a long time over poor connections, this could well be the solution :slight_smile:

Can you also tell me if there is a way to cancel the dhx.delay function?

dhx.delay calls setTimeout. So, you can apply clearTimeout:

var t = dhx.delay(…);
window.clearTimeout(t);

Looks like it is using window.setTimeout() so we should be able to do the following:

var timeoutID = dhx.delay( function(){dhx.alert("BOOM!!!")} ,5000 ); window.clearTimeout( timeoutID );

OK, I am struggling to find a structure that works reliably. The xhr.timeout parameter does not seem to always work.

I think I am going to have to give up on the DHTMLx Touch ajax functions and include jQuery in my app.

Jamesbe, I’ve been struggling with this too, here’s what I’m doing at the moment …

var timedOut = false;
var callBack = {
  error: function(t,x,q)
  {
    if (timedOut)
    {
      alert("Timed out out getting data from server");
    }
    else
    {
      alert("Communication error getting data from server");
    }
  },
  success: function(t,x,q)
  {
    if (q.responseText.length)
    {
      // got a response from server
      // do something with it here
    }
    else
    {
      alert("Server response contained no data");
    }
  }
};

var httpObj = dhx.ajax("http://url-to-get-ajax-response-from",callBack);
var timeOut = setTimeout(function(){
  timedOut = true;
  httpObj.abort();
},60000);

I assign the dhx.ajax() object to a variable, then set a timeout to call that object’s abort() method if nothing’s happened within a pre-determined time (in this case 60000ms = 1 minute).

In my testing DHTMLX does not always call the error function when it times out, so I test the response text in the success function to see if anything has come back.

One thing to note is that if you use the synchronous version of the AJAX call nothing happens, because sync means the browser is suspended until the AJAX call completes, which means Javascript is suspended, which means you can’t programmatically abort the call if it’s taking too long.

// this doesn't work ...
var httpObj = dhx.ajax().sync().post(URL, data, callBack);
var timeOut = setTimeout(function(){
  httpObj.abort();
},60000);