javascript XmlHttpRequest onprogress 间隔
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5495625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XmlHttpRequest onprogress interval
提问by Oscar
I'm using XmlHttpRequests to upload images to a server and I'd like to show the user the progress of these uploads.
我正在使用 XmlHttpRequests 将图像上传到服务器,我想向用户显示这些上传的进度。
Unfortunately the interval between calls to my onprogress-event handler is too large. Usually onprogress is called only once or twice for a 500k image.
不幸的是,对我的 onprogress-event 处理程序的调用间隔太大。对于 500k 图像,通常 onprogress 只调用一次或两次。
Here is my code:
这是我的代码:
/* This function is not called often enough */
function progress(e){
console.log('Uploading: ' + Math.round((e.loaded / e.total) * 100) + ' %');
}
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress', progress, false);
xhr.send(data);
Can this behaviour be changed or is this hardcoded somewhere in the browser implementation?
可以更改此行为还是在浏览器实现中的某处进行硬编码?
采纳答案by Matt Bierner
The W3 sets forth the following guidelines in their XMLHttpRequest Level 2document. Obviously varying levels of conformance across browsers are to be expected.
W3 在其XMLHttpRequest Level 2文档中阐述了以下指南。显然,不同浏览器的一致性水平是可以预期的。
Uploads:
上传:
While the request entity body is being uploaded and the upload complete flag is false, queue a task to fire a progress event named progress at the XMLHttpRequestUpload object about every 50ms or for every byte transmitted, whichever is least frequent. - W3 XMLHttpRequest Level 2 (Bolded for emphasis)
当请求实体主体正在上传并且上传完成标志为 false 时,将一个任务排队,以大约每 50 毫秒或传输的每个字节(以频率最低的为准)在 XMLHttpRequestUpload 对象上触发一个名为 progress 的进度事件。- W3 XMLHttpRequest Level 2(加粗以示强调)
Downloads:
下载:
When it is said to make progress notifications, while the download is progressing, queue a task to fire a progress event named progress about every 50ms or for every byte received, whichever is least frequent. - W3 XMLHttpRequest Level 2 (Bolded for emphasis)
当说要发出进度通知时,在下载进行时,将任务排队以大约每 50 毫秒或接收到的每个字节触发一个名为 progress 的进度事件,以频率最低的为准。- W3 XMLHttpRequest Level 2(加粗以示强调)
I am not aware of an api to customize this functionality.
我不知道可以自定义此功能的 api。
回答by user574535
Also, be careful that local debugging HTTP proxies (like Charles, for instance) tend to affect progress events firing interval.
此外,请注意本地调试 HTTP 代理(例如 Charles)往往会影响进度事件触发间隔。