BackgroundFetchRegistration: progress event
Khả dụng hạn chế
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
Note: This feature is available in Web Workers.
Sự kiện progress của giao diện BackgroundFetchRegistration được kích hoạt khi tác vụ tìm nạp nền liên quan có tiến triển.
Trên thực tế, sự kiện này được kích hoạt khi bất kỳ thuộc tính nào sau đây trả về một giá trị mới:
uploaded,downloaded,result, hoặcfailureReason.
Cú pháp
Dùng tên sự kiện trong các phương thức như addEventListener(), hoặc thiết lập một thuộc tính xử lý sự kiện.
addEventListener("progress", (event) => { })
onprogress = (event) => { }
Kiểu sự kiện
Một Event tổng quát không có thuộc tính bổ sung nào.
Ví dụ
Ví dụ sau minh họa cách ghi lại tiến trình của một lượt tải xuống. Trước tiên, mã sẽ kiểm tra xem downloadTotal đã được cung cấp khi tác vụ tìm nạp nền được đăng ký hay chưa. Giá trị này sau đó được dùng để tính phần trăm dựa trên thuộc tính downloaded.
bgFetch.addEventListener("progress", () => {
if (!bgFetch.downloadTotal) return;
const percent = Math.round(
(bgFetch.downloaded / bgFetch.downloadTotal) * 100,
);
console.log(`Download progress: ${percent}%`);
});
Thông số kỹ thuật
| Thông số kỹ thuật |
|---|
| Background Fetch> # background-fetch-registration-events> |