IntersectionObserver: unobserve() method

Baseline Widely available

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

Phương thức unobserve() của giao diện IntersectionObserver yêu cầu IntersectionObserver dừng quan sát phần tử đích được chỉ định.

Cú pháp

js
unobserve(target)

Tham số

target

Element cần dừng quan sát. Nếu phần tử được chỉ định không đang được quan sát, phương thức này không làm gì và không có ngoại lệ nào được ném ra.

Giá trị trả về

Không có (undefined).

Ví dụ

Đoạn mã này minh họa việc tạo một trình quan sát, quan sát một phần tử, rồi dừng quan sát phần tử đó.

js
const observer = new IntersectionObserver(callback);
observer.observe(document.getElementById("elementToObserve"));

// …

observer.unobserve(document.getElementById("elementToObserve"));

Thông số kỹ thuật

Specification
Intersection Observer
# dom-intersectionobserver-unobserve

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

Xem thêm