0

I am firing an ajax call to one of my rails server using basic javascript call.

I want to know if there is a way to monitor the ongoing ajax call so that if it takes a lot of time to get the response , I can show an alert saying "Request taking lot of time. Please wait"

Please help.

Thanks

Vipul Kumar
  • 259
  • 1
  • 2
  • 12

1 Answers1

2

Just use a timer, that you clear when the call is done

var timer = null;

$.ajax({
    url  : 'someurl',
    data : {data: 'data'},
    onbeforesend : function() {
        timer = setTimeout(function() {
            alert('Request taking lot of time. Please wait')
        }, 2000);
    },
    timeout : 10000
}).always(function() {
    clearTimeout(timer);
});

The timeout setting aborts the call if takes too long.

adeneo
  • 312,895
  • 29
  • 395
  • 388