Source code
Revision control
Copy as Markdown
Other Tools
Test Info:
- This WPT test may be referenced by the following Test IDs:
- /intersection-observer/v2/delay-without-track-visibility.html - WPT Dashboard Interop Dashboard
<!DOCTYPE html>
<meta name="viewport" content="width=device-width,initial-scale=1">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../resources/intersection-observer-test-utils.js"></script>
<style>
body, html {
margin: 0;
}
#target {
position: absolute;
top: 0;
width: 100px;
height: 100px;
background-color: green;
}
</style>
<div id="target"></div>
<script>
promise_test(async t => {
let entries = [];
const delay = 100;
const target = document.getElementById("target");
const observer = new IntersectionObserver(changes => {
entries = entries.concat(changes);
}, { delay });
t.add_cleanup(() => observer.disconnect());
observer.observe(target);
// The first notification should be sent without delay.
await waitForNotification(t);
assert_equals(entries.length, 1);
assert_true(entries[0].isIntersecting);
target.style.top = "-200px";
while (true) {
const timestamp = await new Promise(resolve => requestAnimationFrame(resolve));
if (timestamp - entries[0].time >= delay) {
break;
}
assert_equals(entries.length, 1, "No notifications before the delay");
}
await waitForNotification(t);
assert_equals(entries.length, 2);
assert_greater_than_equal(entries[1].time - entries[0].time, delay);
assert_false(entries[1].isIntersecting);
target.style.top = "0";
// This style change causes a frame to run before the observer can generate
// a notification. Make sure the notification is still generated without
// forcing more frames.
await new Promise(resolve => t.step_timeout(resolve, 2 * delay));
assert_equals(entries.length, 3);
assert_true(entries[2].isIntersecting);
assert_greater_than_equal(entries[2].time - entries[1].time, delay);
}, "'delay' throttles the frequency of notifications without trackVisibility");
</script>