jQuery v1.4 中的 onreadystatechange
新的 jQuery v1.4 是这么说的...
jQuery.ajax() 现在正在使用 onreadystatechange 而不是计时器
Ajax 请求现在应该使用更少的资源 onreadystatechange 而不是轮询。
我不确定这到底意味着什么,但在我的网站(社交网络,如 facebook、myspace)上,我有用户收到的通知,它会定期进行 AJAX 调用以查看是否有新的通知要显示在页面上,这是否可以改进类似事情的完成方式?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,它会改善您的网站。根据您的站点使用的 AJAX 数量,速度方面的改进不会像调用期间使用的系统资源减少那样明显。
IE6 不支持
onreadystatechange
所以我假设它会回退到 IE6 的计时器,但 XMLHTTPRequest 的大多数其他浏览器实现都支持该事件。事件回调始终使用比轮询脚本(每隔几毫秒检查一次)更少的资源。关于IE6
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
2 Doug Neiner
IE 6 绝对支持 onreadystatechange 事件。我现在可以在 XPsp2 上的 IE 6.0.2 中看到它。
MSDN 声明自 IE 版本 7 起支持对象 XMLHttpRequest,包括其所有事件。现代库(如 jQuery)通过手动创建对象“Microsoft.XMLHTTP”或“Msxml2.XMLHTTP”来解决此问题,它们都支持 onreadystatechange 事件。
示例此处 (MSDN)。
祝你好运。
PS 在这里回答,因为此线程位于搜索“onreadystatechange jquery”的 Google 结果的前 10 名中
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
是xmlHttpRequest
对象的一个属性。看起来 $.ajax 之前正在轮询 xhr 以查看它是否已准备好,但现在正在侦听实际事件。这意味着它消耗更少的 javascript 资源,它不会减轻您的任何服务器资源,因为您仍然需要在这个意义上进行 AJAX 轮询。因此,这实际上并不是您必须实施的新技术或其他什么,这只是后面发生的事情的差异,并且要利用该改进,您所要做的就是切换到新版本。我猜测(但我不确定),现在他们使用
onReadyStateChange
,您还可以将自己的回调方法传递给该事件,这将允许您执行 Comet(“AJAX Push”),这可能会提高您的服务器端性能。但请注意,Comet 的实现可能很棘手:)onReadyStateChange
is a property on thexmlHttpRequest
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 :)