Faux Timeout Triggers with JavaScript/jQuery & AJAX
Within jQuery.ajax we have the blessing of 开发者_Go百科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?
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
}
});
精彩评论