Question

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?

Was it helpful?

Solution

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

OTHER TIPS

2 Doug Neiner

IE 6 definitely supports event onreadystatechange. I can see it right now in IE 6.0.2 on XPsp2. MSDN states that object XMLHttpRequest supported since version 7 of IE, including all of its events. Modern libraries (like jQuery) workaround this by manual creation of objects "Microsoft.XMLHTTP" or "Msxml2.XMLHTTP" which are both supporting onreadystatechange event. example here (MSDN).

Good luck.

PS answered here coz this thread in top 10 of google results searching "onreadystatechange jquery"

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 :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top