Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

IdleDeadline — an idle callback's time budget

IdleDeadline is the object passed to a requestIdleCallback() callback. It exposes the estimated remaining time and indicates whether the callback ran because its timeout expired.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

window.requestIdleCallback((deadline) => { deadline.didTimeout; deadline.timeRemaining(); }, { timeout: 1000 });

IdleDeadline is a Web API interface exposed on Window, not part of ECMAScript. The browser supplies it to a requestIdleCallback() callback; do not construct it with new IdleDeadline(). Feature-detect the API; Node 24 does not establish browser support.

IdleDeadline.timeRemaining() returns estimated remaining milliseconds as a nonnegative DOMHighResTimeStamp, potentially fractional. Once the deadline is reached, it returns 0. Check it between small work units: the deadline does not interrupt executing code.

IdleDeadline.didTimeout is a read-only boolean. true means invocation due to expiration of the timeout option; in that case, timeRemaining() returns 0. Merely exhausting the budget does not change didTimeout to true. timeout does not guarantee an exact execution time.

The example sums numbers in chunks and reschedules unfinished work. Its didTimeout path permits one small operation, not an unbounded loop. This cooperatively shares the main thread; it does not run on a separate thread.

Examples

(() => {
  if (typeof window.requestIdleCallback !== "function") {
    console.log("requestIdleCallback unavailable");
    return;
  }

  const values = [1, 2, 3, 4];
  let index = 0;
  let total = 0;

  function work(deadline) {
    let processed = 0;
    while (
      index < values.length &&
      (deadline.timeRemaining() > 1 ||
        (deadline.didTimeout && processed === 0))
    ) {
      total += values[index++];
      processed++;
    }

    if (index < values.length) {
      window.requestIdleCallback(work, { timeout: 1000 });
    } else {
      console.log(total);
    }
  }

  window.requestIdleCallback(work, { timeout: 1000 });
})();

Result

10

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources