HTMLMediaElement: sự kiện progress

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Sự kiện progress được kích hoạt định kỳ khi trình duyệt tải tài nguyên.

Sự kiện này không thể hủy và không bong bóng.

Cú pháp

Sử dụng tên sự kiện trong các phương thức như addEventListener() hoặc đặt thuộc tính xử lý sự kiện.

js
addEventListener("progress", (event) => { })

onprogress = (event) => { }

Kiểu sự kiện

Một Event chung chung.

Ví dụ

Ví dụ trực tiếp

HTML

html
<div class="example">
  <button type="button">Load video</button>
  <video controls width="250"></video>

  <div class="event-log">
    <label for="eventLog">Event log:</label>
    <textarea readonly class="event-log-contents" id="eventLog"></textarea>
  </div>
</div>

Javascript

js
const loadVideo = document.querySelector("button");
const video = document.querySelector("video");
const eventLog = document.querySelector(".event-log-contents");
let source = null;

function handleEvent(event) {
  eventLog.textContent += `${event.type}\n`;
}

video.addEventListener("loadstart", handleEvent);
video.addEventListener("progress", handleEvent);
video.addEventListener("canplay", handleEvent);
video.addEventListener("canplaythrough", handleEvent);

loadVideo.addEventListener("click", () => {
  if (source) {
    document.location.reload();
  } else {
    loadVideo.textContent = "Reset example";
    source = document.createElement("source");
    source.setAttribute(
      "src",
      "https://mdn.github.io/learning-area/html/multimedia-and-embedding/video-and-audio-content/rabbit320.mp4",
    );
    source.setAttribute("type", "video/mp4");

    video.appendChild(source);
  }
});

Kết quả

Đặc tả kỹ thuật

Thông số kỹ thuật
HTML
# event-media-progress
HTML
# handler-onprogress

Tương thích trình duyệt

Xem thêm