views:

424

answers:

1

Within jQuery.ajax we have the blessing of setting a timeout in milliseconds and an Error callback to process that timeout.

However, the some people are simply on slow connection, with small amounts of patience. What I want to do is simply display a message stating "This is taking longer than usual".

The Timeout arguement in jQuery won't satisfy this, and setTimeout() does exactly the same thing. How could this be achieved with a simple time check?

A: 

OK, this was simple enough.

All I needed to do was actually set up an independent Timout, with a function inside to display whatever message I needed to.

You can still keep in the Timeout/Error Callback for really long extended periods, too.

var timeout = true;

timeout = setTimeout(function() {
 if (timeout) {
  $("#zendesk-dropbox-error").html("Contacting the Helpdesk is taking longer than usual, try submitting manually?");
 }
}, 9000);


// Call for a JSON return from the PHP script
$.ajax({ type: 'GET', url: http://www.example.com, dataType: 'json', cache: false, data: ({ a: 'b' }), timeout: 60000, 
    success: function(zendesk){
        timeout = false;
        // Code
    },error: function(objAJAXRequest, strError) {
        // Code
    }
});
jakeisonline
This works only for one ajax request but not on smart phones where some requests are slow and some are fast... i thought you want to be more accurate than this..
Ghommey
That's fine, since it won't be available for smart phones - it's part of a much bigger app which Smart Phones couldn't possibly handle. It's an isolated, modular AJAX request and as such will be the only one running.
jakeisonline