Last week, I had to create a page that would monitoring another server for up-time (during an intensive data transfer process that was crashing out the server from time to time and would need to be re-started). When coding this page, I used the jQuery AJAX Timeout setting for the first time. It was a nice little feature. I am not sure how long an AJAX request would wait before timing out if you exclude the Timeout setting; I wonder if it would ever timeout if the request simply kept hanging?
Anyway, I thought I would just throw together a quick demo so you can see part of what I was doing. First, I am going to set up a simple ColdFusion page that allows the page to be executed with an arbitrary sleep duration:
<!--- Param the number of milliseconds to pause. ---> <cfparam name="URL.timeout" type="numeric" default="1000" /> <!--- Pause the thread. ---> <cfthread action="sleep" duration="#URL.timeout#" /> <!--- Return JSON success. ---> <cfcontent type="text/plain" variable="#ToBinary( ToBase64( SerializeJSON( true ) ) )#" />
As you can see, this page simply sleeps the current thread and then returns a JSON response.
Now, I set up a simple page that makes an AJAX request to the above ColdFusion page making sure to get the target page to sleep longer than the AJAX timeout setting (so as to force a timeout error):
As you can see in the AJAX properties, we are getting our target page to sleep for 5,000 milliseconds but we are only allowing a 2,000 millisecond timeout for the AJAX request. When we run this page, the following content is loaded into the DOM:
Error! Type: timeout
Based on the resultant DOM content, we can see that the Timeout setting caused the AJAX request to end in error with the error handler being invoked. Just a small demo, but I think this is a setting that I want to start working into all of my jQuery AJAX applications.
Want to use code from this post? Check out the license.