views:

905

answers:

2

The new jQuery v1.4 says this...

jQuery.ajax() is now using onreadystatechange instead of a timer

Ajax requests should now take fewer resources by using onreadystatechange instead of polling.

I am not sure exactly what this means but on my site (social network like facebook, myspace) I have notifications that user's get, it makes AJAX call periodicly to see if there is new notifications to show on a page, is this something that can improve the way stuff like that is done?

+2  A: 

Yes it would improve your site. Depending on how much AJAX your site uses, the improvement will not be noticeable in speed as much as lower system resources being used during the call.

IE6 does not support onreadystatechange so I assume it will fall back to the timer for IE6, but most other browser implementations of XMLHTTPRequest support that event. Event callbacks always use less resources than a polling script (that checks something every few milliseconds).

Regarding IE6

The onreadystatechange event was introduced in Windows Internet Explorer 7. Source: MSDN

Doug Neiner
Can you provide a source that states that IE6 does not support onreadystatechange?
Vincent Robert
@Vincent, yes, question updated to include it.
Doug Neiner
@Jasondavis did you have any more questions about this?
Doug Neiner
A: 

onReadyStateChange is a property on the xmlHttpRequest object. It looks like $.ajax was previously polling the xhr to see if it was ready, but is now instead listening to the actual event. This would mean that it consumes less javascript resources, it does not relieve you of any server resources, because you still have to do AJAX polling in that sense. So it's not really a new technique that you have to implement or something, it's just a difference in what goes on behind, and to utilize that improvement, all you have to do is to switch to the new version.

I'm guessing (but I'm not sure), that now that they use onReadyStateChange, you can also pass your own callback methods to that event, which would allow you to do Comet ("AJAX Push"), which could potentially improve your server-side performance. But be aware that Comet can be tricky to implement :)

David Hedlund