Time-out with jQuery.get()

use ajaxSetup

$.ajaxSetup({
timeout:2000 // in milliseconds 
});

//your get request here

Call $.ajax and pass the timeout option.


$.get is just a nice shorthand for $.ajax which has a timeout option, which will specify how long the script will wait before cancelling the callback.

You can override the global default using $.ajaxSetup({timeout: 9001}), which will allow you to continue to use $.get.

If you simply want to alert the user without cancelling the request, use $.ajax, set a timeout, and cancel the timeout in the complete callback.

Here's some example starter code, i haven't tested any of it, and it could probably be turned into a plugin with a bit of work:

(function(){
  var t, delay;
  delay = 1000;
  $('.delay-message').ajaxStart(function () {
  var $this;
  $this = $(this);
  t = setTimeout(function () {
    $this.trigger('slowajax');
  }, delay);
  }).ajaxComplete(function () {
    if (t) {
      clearTimeout(t);
    }
  });
}());

$('.delay-message').on('slowajax', function () {
  $(this).show().delay(2000).fadeOut(1000);
});

Just use $.ajax(...):

$.ajax({
   url: url,
   success: function(data){
        //...
   },
   timeout: 1000 //in milliseconds
});

Also as stated below in the comments you can use .ajaxSetup(...) to apply the timeout globally:

$.ajaxSetup({timeout:1000}); //in milliseconds

Tags:

Html

Ajax

Jquery