MCPcopy Create free account
hub / github.com/denoland/std / setArbitraryLengthTimeout

Function setArbitraryLengthTimeout

async/delay.ts:82–107  ·  view source on GitHub ↗
(
  callback: () => void,
  delay: number,
)

Source from the content-addressed store, hash-verified

80const I32_MAX = 2 ** 31 - 1;
81
82function setArbitraryLengthTimeout(
83 callback: () => void,
84 delay: number,
85): { valueOf(): number } {
86 // ensure non-negative integer (but > I32_MAX is OK, even if Infinity)
87 delay = Math.trunc(Math.max(delay, 0) || 0);
88
89 if (delay <= I32_MAX) {
90 const id = Number(setTimeout(callback, delay));
91 return { valueOf: () => id };
92 }
93
94 const start = Date.now();
95 let timeoutId: number;
96
97 const queueTimeout = () => {
98 const currentDelay = delay - (Date.now() - start);
99 timeoutId = currentDelay > I32_MAX
100 ? Number(setTimeout(queueTimeout, I32_MAX))
101 : Number(setTimeout(callback, currentDelay));
102 };
103
104 queueTimeout();
105
106 return { valueOf: () => timeoutId };
107}

Callers 1

delayFunction · 0.85

Calls 3

queueTimeoutFunction · 0.85
maxMethod · 0.80
nowMethod · 0.80

Tested by

no test coverage detected