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?
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);
Get a guaranteed answer from DHTMLX technical support team
under the most suitable support plan