views:

272

answers:

2

Hi

I think that you can set like on post and get methods in jquery timeouts but I am wondering does jquery have a global timeone like it has with ajax start and stop.

Like I would like to set it that if say a post or get or some sort of ajax request is running and it runs more then X amount of seconds. That a popup or something in that nature comes up saying that the "server is slow and the request has timed out" and kills that request.

Then the user can either try again or do something else.

Does jquery have something like this?

Thanks

+2  A: 

Yep, jQuery has a 'timeout' property (in milliseconds) you send the $.ajax() event:

http://www.bennadel.com/blog/1500-Catching-Timeout-Errors-With-jQuery-Powered-AJAX.htm

$.ajax(
  {
   method: "get",
   url: "yourpage.php",
   dataType: "json",
   timeout: (3 * 1000), // 3 seconds

   success: function(){
    //success code here
   },

   error: function( request, strError ){
    //error code here
   }
  }
);
Ryan Doherty
+2  A: 

The jQuery.ajax function is what you want. It gives you access to a pretty large set of options for making AJAX calls.

Specifically, you're going to want to make a call similar to

$.ajax({'complete': callbackFunction, 'url': 'foo/bar/', 'timeout': 5000, 'error': errorCallback});

The two options you're interested in are timeout and error. The entire documentation for the function is here. It's a bit more work than using the more standard get/post functions, but much more flexible.

The error function is very similar to the standard callback you use with any jQuery AJAX request. While the callback is called if the request succeeds, the error function is called when it fails (such as 404 errors, or when the timeout is hit). You'd use the error function to display your message to the user that their request has timed out. Full documentation on the function's arguments (which practically speaking, you probably won't need to use) is available on the $.ajax doc page (linked earlier).

Alternatively, you can set the timout globally on every AJAX call by using the jQuery.ajaxSetup function. Its arguments are exactly the same as the jQuery.ajax function, so you'd do something like this:

$.ajaxSetup({'timeout': 5000, 'error': errorCallback});

The upside is that you can continue using jQuery.get/jQuery.post, but the downside is that you have to do extra work to make AJAX calls without a timeout.

ShZ
Cool I think for now ajaxSetup is what I want otherwise I got to redue all my posts. If it becomes problem then I can convert. What would happen if you set a timeout in ajaxSetup and one in $.ajax? which one would win?Also how about different connections like I have a 6mb connection and almost all my ajax request take about 500ms or less to do. But how about a 56K modem. What would be a safe timeout 10seconds? Also I am still not clear what 'error' really does.
chobo2
If you're using the timeout in $.ajaxSetup, then yeah, you can use a different value by using $.ajax.I honestly couldn't say what a reasonable response time would be for different speeds with any kind of authority, but 10s seems like a good amount.I've clarified the usage of the error function in the answer.
ShZ