XHR 的“readyState==3"怎么办?工作? [英] How dos XHR's "readyState==3" work?

查看:53
本文介绍了XHR 的“readyState==3"怎么办?工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码片段

<预>xhr.onreadystatechange = function(){如果(xhr.readyState == 3){console.log("readyState 响应长度" + xhr.response.length);}}

控制台是

<预>readyState 响应长度:3854readyState 响应长度:33214readyState 响应长度:35296

我不知道 onreadystatechange 触发的时间是什么,它似乎与响应的大小有关,它是如何工作的?

解决方案

什么是 XHR readyState=3?

拥有 readyState3 的值表示当前状态为 LOADING.

<块引用>

那么该状态的 readyStateChange 事件何时触发?

每当异步请求切换到 LOADING 状态时.尤其是触发:

<块引用>

一旦接收到响应实体主体的第一个字节(或更多) […] 或如果没有响应实体主体 […]然后切换到 LOADING 状态.

因此浏览器可以在收到响应字节时自由地使用 readyState 3 触发此事件.有些更频繁地触发它(在每次缓冲区刷新时),有些则不会.规范甚至说对于readystatechange事件:

<块引用>

由于历史原因,readyState 属性在某些看似随意的时间发生变化.

Here's the code snippet

xhr.onreadystatechange = function(){ 
   if(xhr.readyState == 3){
      console.log("readyState response length " + xhr.response.length);
   }
} 

And the console is

readyState response length: 3854 
readyState response length: 33214 
readyState response length: 35296 

I don't know what's the timing of onreadystatechange firing, it does seems a bit related to the size of response,how does it work?

解决方案

What is XHR readyState=3?

Having the readyState with a value of 3 it means that the current state is LOADING.

So when is a readyStateChange event for that state fired?

Whenever an asynchrounous request does switch to the LOADING state. That is especially triggered by:

Once the first byte (or more) of the response entity body has been received […] or If there is no response entity body […] Then switch to the LOADING state.

So browsers are free to fire this event with readyState 3 when they receive bytes of the response. Some trigger it more often (at every buffer flush), other's dont. The spec even says for the readystatechange Event:

The readyState attribute changes at some seemingly arbitrary times for historical reasons.

这篇关于XHR 的“readyState==3"怎么办?工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆