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

Method delay

src/service/timer-impl.js:53–85  ·  view source on GitHub ↗

* Runs the provided callback after the specified delay. This uses a micro * task for 0 or no specified time. This means that the delay will actually * be close to 0 and this will NOT yield to the event queue. * * Returns the timer ID that can be used to cancel the timer (cancel method).

(callback, opt_delay)

Source from the content-addressed store, hash-verified

51 * @return {number|string}
52 */
53 delay(callback, opt_delay) {
54 if (!opt_delay) {
55 // For a delay of zero, schedule a promise based micro task since
56 // they are predictably fast.
57 const id = 'p' + this.taskCount_++;
58 this.resolved_
59 .then(() => {
60 if (this.canceled_[id]) {
61 delete this.canceled_[id];
62 return;
63 }
64 callback();
65 })
66 .catch(reportError);
67 return id;
68 }
69 const wrapped = () => {
70 try {
71 callback();
72 } catch (e) {
73 reportError(e);
74 throw e;
75 }
76 };
77 const index = this.win.setTimeout(wrapped, opt_delay);
78 if (getMode().test) {
79 if (!timersForTesting) {
80 timersForTesting = [];
81 }
82 timersForTesting.push(index);
83 }
84 return index;
85 }
86
87 /**
88 * Cancels the previously scheduled callback.

Callers 15

promiseMethod · 0.95
timeoutPromiseMethod · 0.95
playMethod · 0.80
test-timer.jsFile · 0.80
buildInternalMethod · 0.80
scheduleMethod · 0.80
maybePumpEarlyFrameFunction · 0.80
broadcast_Method · 0.80
closeShadowRootAsync_Method · 0.80
constructorMethod · 0.80
url_Method · 0.80
scrollToElement_Method · 0.80

Calls 5

getModeFunction · 0.90
catchMethod · 0.80
callbackFunction · 0.50
thenMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected