MCPcopy Create free account
hub / github.com/ampproject/amphtml / onDocumentState

Function onDocumentState

third_party/subscriptions-project/config.js:66–83  ·  view source on GitHub ↗

* Calls the callback once when document's state satisfies the condition. * @param {!Document} doc * @param {function(!Document):boolean} condition * @param {function(!Document)} callback

(doc, condition, callback)

Source from the content-addressed store, hash-verified

64 * @param {function(!Document)} callback
65 */
66function onDocumentState(doc, condition, callback) {
67 if (condition(doc)) {
68 // Execute callback right now.
69 callback(doc);
70 return;
71 }
72
73 // Execute callback (once!) after condition is satisfied.
74 let callbackHasExecuted = false;
75 const readyListener = () => {
76 if (condition(doc) && !callbackHasExecuted) {
77 callback(doc);
78 callbackHasExecuted = true;
79 doc.removeEventListener('readystatechange', readyListener);
80 }
81 };
82 doc.addEventListener('readystatechange', readyListener);
83}
84
85/**
86 * Returns a promise that is resolved when document is ready.

Callers 1

onDocumentReadyFunction · 0.70

Calls 2

callbackFunction · 0.50
addEventListenerMethod · 0.45

Tested by

no test coverage detected