一、onprogress事件
onprogress事件是XMLHttpRequest對象的一個屬性,用於在數據從伺服器中接收時周期性地觸發,在整個數據傳輸過程中,可用該屬性顯示傳輸的進度。由於onprogress 具有周期性觸發的特點,因此適用於大文件或網路環境較差的情況。在onprogress事件中,可以通過event對象獲取到傳輸的相關信息,如已接收的位元組數、已傳輸的百分比等。
function myFunction() { var xhr = new XMLHttpRequest(); xhr.onprogress = function(event) { if (event.lengthComputable) { var percentComplete = (event.loaded / event.total) * 100; console.log(percentComplete + '% downloaded'); } else { console.log('Unable to compute progress information since the total size is unknown'); } }; xhr.open('GET', 'https://www.example.com/large_file'); xhr.send(); }
二、progress
progress事件與onprogress事件類似,都用於獲取傳輸的進度信息。progress事件是XMLHttpRequest對象的另一個屬性,不同於onprogress事件,progress事件在數據從伺服器中接收時持續觸發,而不是周期性觸發。
function myFunction() { var xhr = new XMLHttpRequest(); xhr.addEventListener('progress', function(event) { if (event.lengthComputable) { var percentComplete = (event.loaded / event.total) * 100; console.log(percentComplete + '% downloaded'); } else { console.log('Unable to compute progress information since the total size is unknown'); } }, false); xhr.open('GET', 'https://www.example.com/large_file'); xhr.send(); }
三、onprogress是什麼意思
onprogress是XMLHttpRequest對象的一個屬性,在數據從伺服器中接收時周期性地觸發。可以在onprogress事件中獲取到傳輸的相關信息。
四、onprogresschanged
onprogresschanged是在onprogress事件中的一個函數,用於檢測傳輸的進度是否發生改變。在數據傳輸過程中,通過調用該函數,可以實時獲取數據的傳輸情況。
function myFunction() { var xhr = new XMLHttpRequest(); xhr.onprogress = function(event) { if (event.lengthComputable) { var percentComplete = (event.loaded / event.total) * 100; console.log(percentComplete + '% downloaded'); if (percentComplete === 100) { console.log('Download complete'); } else { onprogresschanged(percentComplete); } } else { console.log('Unable to compute progress information since the total size is unknown'); } }; xhr.open('GET', 'https://www.example.com/large_file'); xhr.send(); }
五、onprogresschanged循環調用
在數據傳輸過程中,可以通過循環調用onprogresschanged函數,實時獲取數據的傳輸情況。
function myFunction() { var xhr = new XMLHttpRequest(); var intervalID; xhr.onprogress = function(event) { if (event.lengthComputable) { var percentComplete = (event.loaded / event.total) * 100; console.log(percentComplete + '% downloaded'); if (percentComplete === 100) { console.log('Download complete'); clearInterval(intervalID); } else { intervalID = setInterval(function() { onprogresschanged(percentComplete); }, 1000); } } else { console.log('Unable to compute progress information since the total size is unknown'); } }; xhr.open('GET', 'https://www.example.com/large_file'); xhr.send(); }
六、onprogresschanged newProgress
onprogresschanged和newProgress是兩個函數,用於在訪問外部資源時實時更新進度條。
function myFunction() { var xhr = new XMLHttpRequest(); var intervalID; xhr.onprogress = function(event) { if (event.lengthComputable) { var percentComplete = (event.loaded / event.total) * 100; newProgress(percentComplete); if (percentComplete === 100) { console.log('Download complete'); clearInterval(intervalID); } else { intervalID = setInterval(function() { onprogresschanged(percentComplete); }, 1000); } } else { console.log('Unable to compute progress information since the total size is unknown'); } }; xhr.open('GET', 'https://www.example.com/large_file'); xhr.send(); } function newProgress(percent) { var progressBar = document.getElementById('progressBar'); progressBar.style.width = percent + '%'; }
七、onprogress表示正在讀取時觸發
在數據從伺服器中接收時,onprogress事件會周期性地觸發,表示正在讀取數據。在onprogress事件中,可以通過event對象獲取到傳輸的相關信息,如已接收的位元組數、已傳輸的百分比等。
function myFunction() { var xhr = new XMLHttpRequest(); xhr.onprogress = function(event) { if (event.lengthComputable) { var percentComplete = (event.loaded / event.total) * 100; console.log(percentComplete + '% downloaded'); } else { console.log('Unable to compute progress information since the total size is unknown'); } }; xhr.open('GET', 'https://www.example.com/large_file'); xhr.send(); }
八、總結
onprogress事件在數據傳輸過程中周期性觸發,用於獲取傳輸的相關信息,如已接收的位元組數、已傳輸的百分比等。progress事件與onprogress事件類似,也可用於獲取傳輸的進度信息,不過它是持續觸發的。onprogresschanged函數可以檢測傳輸的進度是否發生改變,從而實現實時更新進度條的效果。onprogress表示正在讀取數據時觸發,可在這個事件中獲取到傳輸的相關信息。在實際開發中,根據需求選擇合適的事件,且應注意優化數據傳輸體驗。
原創文章,作者:VPSVO,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/361802.html