MCPcopy
hub / github.com/ampproject/amphtml / onDocumentState

Function onDocumentState

src/core/document/ready.js:40–56  ·  view source on GitHub ↗

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

(doc, stateFn, callback)

Source from the content-addressed store, hash-verified

38 * @param {function(Document):void} callback
39 */
40function onDocumentState(doc, stateFn, callback) {
41 let ready = stateFn(doc);
42 if (ready) {
43 callback(doc);
44 } else {
45 const readyListener = () => {
46 if (stateFn(doc)) {
47 if (!ready) {
48 ready = true;
49 callback(doc);
50 }
51 doc.removeEventListener('readystatechange', readyListener);
52 }
53 };
54 doc.addEventListener('readystatechange', readyListener);
55 }
56}
57
58/**
59 * Returns a promise that is resolved when document is ready.

Callers 2

onDocumentReadyFunction · 0.70
whenDocumentCompleteFunction · 0.70

Calls 2

callbackFunction · 0.50
addEventListenerMethod · 0.45

Tested by

no test coverage detected