PeriodicSyncManager: register() method

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.

Phương thức register() của giao diện PeriodicSyncManager đăng ký yêu cầu đồng bộ hóa định kỳ với trình duyệt với tag và tùy chọn được chỉ định. Nó trả về Promise phân giải khi đăng ký hoàn tất.

Cú pháp

js
register(tag, options)

Tham số

tag

Định danh String duy nhất.

options Optional

Một Object chứa dữ liệu tùy chọn sau:

minInterval

Khoảng thời gian tối thiểu, tính bằng mili giây, mà tại đó đồng bộ hóa định kỳ sẽ xảy ra.

Giá trị trả về

Trả về Promise phân giải với undefined.

Ngoại lệ

InvalidStateError DOMException

Được trả về nếu không có ServiceWorker đang hoạt động.

NotAllowedError DOMException

Được trả về nếu quyền cho đồng bộ hóa định kỳ nền không được cấp.

InvalidAccessError DOMException

Được trả về nếu cửa sổ đang hoạt động không phải là cửa sổ chính (không thuộc loại auxiliary hoặc top-level).

Ví dụ

Hàm bất đồng bộ sau đăng ký một periodic background sync với khoảng thời gian tối thiểu một ngày từ ngữ cảnh duyệt web:

js
async function registerPeriodicNewsCheck() {
  const registration = await navigator.serviceWorker.ready;
  try {
    await registration.periodicSync.register("fetch-news", {
      minInterval: 24 * 60 * 60 * 1000,
    });
  } catch {
    console.log("Periodic Sync could not be registered!");
  }
}

Thông số kỹ thuật

Thông số kỹ thuật
Web Periodic Background Synchronization
# dom-periodicsyncmanager-register

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

Xem thêm