Friday, March 24th, 2006

Ajax Timeouts with Prototype

Category: Programming, Prototype

Network problems are only one of the difficulties that can plague Ajax applications, but they can be the most difficult to deal with. How can you check, with a Javascript call, to see if the remote host you’re using is down? A simple call to the server won’t work – the connection would just sit there with no update to the user. So, what’s an alternative? This script, using prototype, that checks the server and, with the help of a timeout setting, can gracefully let the user know it’s inaccessible.

I’ve been implementing some AJAX goodness in Mayday and other FastFrame apps lately. In my reading of the various pitfalls of AJAX one that popped up repeatedly was how to handle gracefully a network outage or the webserver going down.

His version borrows ideas from a different post on handling the connection failure, but alters it a bit to get it to cooperate with a prototype setup.

The real key is in the use of responders combined with a setTimeout to see what the readyState of the connection is. If the timer runs out and there’s still no successful connection, the script errors out with a “network is down” message. Of course, this could be a number of things – the server is down, the network connection between you and it is down, etc. At least it’s a step closer to an application that doesn’t care.

Posted by Chris Cornutt at 9:32 am
6 Comments

+++--
3.4 rating from 48 votes

6 Comments »

Comments feed TrackBack URI

I use a similar mechanism to warn users that their session is about to expire, say, if they go to lunch or get distracted by another task. 5 minutes before the server session timeout period ends, a confirm() warns them and asks if they would like to reset. If yes, an ajax call to a simple JSP will reestablish the session, without requiring the user to move away from the current screen, and possibly lose data.

Comment by Marty — March 24, 2006

BTW, your comment script is showing me lots of nasty words, and complaining about bad regexp in the blacklist file. You should test this.

Comment by Marty — March 24, 2006

[…] via Ajaxian […]

Pingback by Spatially Adjusted with James Fee » Blog Archive » Handling AJAX timeouts gracefully — March 24, 2006

Interesting Finds

Trackback by Jason Haley — March 25, 2006

I could not get the codejanitor code to work correctly with prototype. I got mixed responses from IE and FFx. And it kept executing my “onComplete function. Don’t know if that is the desired behavior but clearly wasn’t the “quick solution” for my needs.

Comment by Craig — November 20, 2006

I too have not been able to get the code to work properly. It always triggers onSuccess and onComplete callbacks and it doesn’t work when you use http or https in the url. If you use a relative path for the URL then it works with the exception of the aforementioned callback errors. Wish I could get it working with http and https in the URL that is my only big issue with it.

Comment by Erik — November 30, 2006

Leave a comment

You must be logged in to post a comment.