加载页面的进度

时间:2016-01-08 14:46:25

标签: javascript jquery javascript-events

如何计算装载地点?我看到有状态加载站点的站点,我知道有一个事件upload.onprogress XMLHttpRequest上传:

XMLHttpRequest.upload.onprogress = function(event) {
  console.log( 'Uploaded ' + event.loaded + ' / ' + event.total );
} 

如何做同样的事情,但要从服务器下载(加载)页面/数据?

1 个答案:

答案 0 :(得分:1)

MDN上有example

var oReq = new XMLHttpRequest();

oReq.addEventListener("progress", updateProgress);
oReq.addEventListener("load", transferComplete);
oReq.addEventListener("error", transferFailed);
oReq.addEventListener("abort", transferCanceled);

oReq.open();

// ...

// progress on transfers from the server to the client (downloads)
function updateProgress (oEvent) {
  if (oEvent.lengthComputable) {
    var percentComplete = oEvent.loaded / oEvent.total;
    // ...
  } else {
    // Unable to compute progress information since the total size is unknown
  }
}

function transferComplete(evt) {
  console.log("The transfer is complete.");
}

function transferFailed(evt) {
  console.log("An error occurred while transferring the file.");
}

function transferCanceled(evt) {
  console.log("The transfer has been canceled by the user.");
}