Pawan
Pawan

Reputation: 32331

How to make the Ajax call again in case Time out error occurs

I have a document ready function inside my html file as shown below

$(document).ready(function() 
{
   cust_id = getParameterByName('customer_id');

    var locationAjaxCall = $.ajax({
        type: 'GET',
        url: url+'/OMS/oms1/getlocationscreen?screen_ids=' + screen_ids,
        jsonpCallback: 'jsonCallback',
        cache: true,
        dataType: 'jsonp',
        jsonp: false,
        success: function (response) {

        },
        error: function (xhr, ajaxOptions, thrownError) {
        }
    });
    locationAjaxCall.done(function() {
        $.ajax({
            type: 'GET',
            url: url+'/OMS/oms1/fromscratchmodified?screen_ids=' + screen_ids,
            jsonpCallback: 'jsonCallback',
            dataType: 'jsonp',
            jsonp: false,
             timeout: 5000,
            success: function(response) {
            },
             error: function(x, t, m) {
            if(t==="timeout") {
            alert("got timeout");
            }
    }
});
}).done(function() {
    });
});

For the second Ajax call . i am keeping a timeout for 5 seconds as shown below

My question is in case a timeout occurs during the second Ajax call , how can i make that Ajax call again ??

(I don't want to reload the entire page as i might lose some data which was already set )

Please let me know if its possible ??

Upvotes: 0

Views: 1220

Answers (2)

gothical
gothical

Reputation: 373

You mean like this? Using setInterval and clear the interval when its successful.

  $(document).ready(function() 
    {
       cust_id = getParameterByName('customer_id');

        var locationAjaxCall = $.ajax({
            type: 'GET',
            url: url+'/OMS/oms1/getlocationscreen?screen_ids=' + screen_ids,
            jsonpCallback: 'jsonCallback',
            cache: true,
            dataType: 'jsonp',
            jsonp: false,
            success: function (response) {

            },
            error: function (xhr, ajaxOptions, thrownError) {
            }
        });
        locationAjaxCall.done(function() {
            var intvl=setInterval(function(){
            $.ajax({
                type: 'GET',
                url: url+'/OMS/oms1/fromscratchmodified?screen_ids=' + screen_ids,
                jsonpCallback: 'jsonCallback',
                dataType: 'jsonp',
                jsonp: false,
                 timeout: 5000,
                success: function(response) {
                 clearInterval(intvl);
                },
                 error: function(x, t, m) {
                if(t==="timeout") {
                alert("got timeout");
                }
              }
          });
        },5000);
      }).done(function() {
     });
    });

Upvotes: 1

Jazi
Jazi

Reputation: 6752

Move second AJAX call to some function, maybe? And when timeout occurs, call it again.

function fname() {
    $.ajax({
            type: 'GET',
            url: url+'/OMS/oms1/fromscratchmodified?screen_ids=' + screen_ids,
            jsonpCallback: 'jsonCallback',
            dataType: 'jsonp',
            jsonp: false,
            timeout: 5000,
            success: function(response) {
                // success
            },
            error: function(x, t, m) {
                if(t==="timeout") {
                    alert("got timeout");
                    fname();
                }
            }
    });
}

Upvotes: 1

Related Questions