XmlHttpRequest onprogress 间隔
我正在使用 XmlHttpRequests 将图像上传到服务器,我想向用户显示这些上传的进度.
I'm using XmlHttpRequests to upload images to a server and I'd like to show the user the progress of these uploads.
不幸的是,对我的 onprogress-event 处理程序的调用之间的间隔太大.对于 500k 的图像,通常只调用一次或两次 onprogress.
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.
这是我的代码:
/* 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?
推荐答案
W3 在他们的 XMLHttpRequest 级别 2 文档.显然,跨浏览器的不同级别的一致性是可以预料的.
The W3 sets forth the following guidelines in their XMLHttpRequest Level 2 document. Obviously varying levels of conformance across browsers are to be expected.
上传:
当请求实体正文被上传并且上传完成标志为假时,排队一个任务以在 XMLHttpRequestUpload 对象上触发一个名为 progress 的进度事件,大约每 50 毫秒或传输的每个字节,以最不频繁的为准强>.- W3 XMLHttpRequest Level 2(粗体表示强调)
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)
下载:
如果说要发出进度通知,则在下载过程中,排队一个任务以触发一个名为 progress 的进度事件,大约每 50 毫秒或接收到的每个字节,以最不频繁的为准.- W3 XMLHttpRequest Level 2(粗体表示强调)
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)
我不知道自定义此功能的 api.
I am not aware of an api to customize this functionality.
相关文章