gpt4 book ai didi

javascript - XmlHttpRequest onprogress 间隔

转载 作者:IT王子 更新时间:2023-10-29 03:12:43 25 4
gpt4 key购买 nike

我正在使用 XmlHttpRequests 将图像上传到服务器,我想向用户显示这些上传的进度。

不幸的是,对我的 onprogress-event 处理程序的调用之间的间隔太大。对于 500k 的图像,onprogress 通常只调用一次或两次。

这是我的代码:

/* 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);

是否可以更改此行为或者是否在浏览器实现中的某处进行了硬编码?

最佳答案

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)

下载:

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。

关于javascript - XmlHttpRequest onprogress 间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5495625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com