XmlHttpRequest onprogress 间隔

XmlHttpRequest onprogress interval(XmlHttpRequest onprogress 间隔)

本文介绍了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 间隔

基础教程推荐