問題描述
我正在使用 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 間隔的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!