0

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 ??

Ashish Chopra
  • 1,413
  • 9
  • 23
Pawan
  • 31,545
  • 102
  • 256
  • 434
  • You mean if after 5 secs it's not finished, re-fire it. So why do you have 5 sec limit to begin with? – artm Dec 12 '14 at 11:59

2 Answers2

1

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();
                }
            }
    });
}
Jazi
  • 6,569
  • 13
  • 60
  • 92
1

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() {
     });
    });
gothical
  • 373
  • 1
  • 7
  • setInterval keeps sending the request every 5 seconds and clearInterval stops it from sending it again once there is a response from the ajax call. – gothical Dec 12 '14 at 12:22
  • should i create a new function by name clearInterval() ?? and what should be the logic under it now ?? – Pawan Dec 12 '14 at 12:33
  • clearInterval() is an intrinsic function that clears the timer set by setInterval(). For more information, you can read here http://www.w3schools.com/jsref/met_win_clearinterval.asp. Also, in your code I can't understand a few things you're trying to do. You're calling success: and .done(). Only one of them is necessary. Also, you're chaining the second done to the done of the first ajax call which seems redundant. Can you tell me what you're trying to do and I'll try to help? – gothical Dec 12 '14 at 12:38
  • I have used success and ajax because somecases the ajax responses are gettng mixed up . so i have coded that way . – Pawan Dec 12 '14 at 12:44