XmlHttpRequest onprogress interval(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.
这篇关于XmlHttpRequest onprogress 间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:XmlHttpRequest onprogress 间隔
基础教程推荐
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01