-2

I know about the timeout setting for the ajax call. But what i'm wondering is, is there a way to display a message to the user if an ajax call is still processing but taking longer than x seconds.

E.g.

During an ajax call, if it takes longer than 10 secs tell the user, "call taking longer than expected"

Drew Landgrave
  • 1,515
  • 3
  • 13
  • 23
  • You could use a timer as here: http://stackoverflow.com/questions/4542863/jquery-ajax-call-with-timer Pretty easy to pop up an alert or whatever. If there's a specific method of feedback, there may be a more specific plugin. How do you want to tell the user? – zanerock May 16 '14 at 01:47
  • I answered that like 20 minutes ago: http://stackoverflow.com/questions/23691167/run-function-if-jquery-ajax-waiting-for-respond-long-enough I can't mark as duplicate because the other guy didn't respond to my answer, but I'm fairly sure it works. – Hamza Kubba May 16 '14 at 01:45

2 Answers2

0

I'd say your best bet is to use window.setTimeout for however long you want to wait for before showing your notification, and then add a window.clearTimeout line to your success callback in your $.ajax() call:

var loadingTimeout = window.setTimeout(function() {
  // show your warning here
  alert('Still loading :P');
}, 10000); // 10000ms = 10sec

$.ajax({
  url: 'http://your/url/here',
  dataType: 'json',
  type: 'GET',
  success: function(r) {
    window.clearTimeout(loadingTimeout);
    // your results logic here
  }
})
wvandaal
  • 4,265
  • 2
  • 16
  • 27
  • How about putting the clearTimeout line in the complete callback just in case the ajax call fails? – PeterKA May 16 '14 at 02:14
-1

Sure, just setTimeout() yourself another function that checks some global variable that gets set by the ajax completion callback. In that function, if the ajax call is still outstanding, show a message.

adv12
  • 8,443
  • 2
  • 24
  • 48