skills
skills

Reputation: 327

Ajax request - timeout error/warning

I have this function and it works fine most of the times, but if something irregular happens, the client will not know why the loading gif is always showing .

I would like to know if I can define a 20 second timeout, and if the ajax request is unsuccessful the loading gif disappears and shows an error like "connection lost" or something similar:

function ajaxrequest(){
                var params = [];
                for (var i=0; i < submit.length; i++) {
                    var ele = submit[i];
                    params[i] = ele + '=' + $('#' + ele).attr('value');
                }
                params = params.join('&');
                $(".agendarleft").html("<center><img src='skins/{VAL_SKIN}/styleImages/iloading.gif'></center>");
                $.ajax({
                type: "GET",
                url: "ajaxload/como.php",
                data: params,
                success: function() {
                $(".agendarleft").html("<center><div style='height:50px'><h2>Enviado! Seremos breves na resposta.</h2></div></center><br/><img src='../skins/Sandbox_Light/styleImages/after1.jpg'/></center>");
                }
                });
            }

Upvotes: 1

Views: 4499

Answers (2)

Pit Digger
Pit Digger

Reputation: 9800

Please check timeout parameter here

http://api.jquery.com/jQuery.ajax/

Demo : http://jsfiddle.net/u3f9C/183/

If you changed timeout to 1000 you will see error . For now it should show timeout as timeout value is really low and it times out even before it tried to connect to url.

Upvotes: 2

trueunlessfalse
trueunlessfalse

Reputation: 1233

You can set up an error callback, like this:

$.ajax({}).error(function(){ // remove spinner or do something else });

See: http://api.jquery.com/jQuery.ajax/

Or in the style of your request above:

$.ajax({error: function() {}, success: function() {} });

Upvotes: 0

Related Questions