XHR 的“readyState==3"怎么办?工作? [英] How dos XHR's "readyState==3" work?
问题描述
这是代码片段
<预>xhr.onreadystatechange = function(){如果(xhr.readyState == 3){console.log("readyState 响应长度" + xhr.response.length);}}控制台是
<预>readyState 响应长度:3854readyState 响应长度:33214readyState 响应长度:35296我不知道 onreadystatechange
触发的时间是什么,它似乎与响应的大小有关,它是如何工作的?
什么是 XHR
readyState=3
?
拥有 readyState
3
的值表示当前状态为 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屋!