mozilla/pdf.js · error · TypeError

Second argument of app.setInterval must be a number

Error message

Second argument of app.setInterval must be a number

What it means

app.setInterval's second argument is the repeat interval in milliseconds and must be a number. PDF.js accepts the object form {nMilliseconds} but, after unwrapping, requires a numeric value; otherwise it throws TypeError. This prevents non-numeric delays reaching the host scheduler.

Source

Thrown at src/scripting_api/app.js:618

      cDefault = cQuestion.cDefault;
      cQuestion = cQuestion.cQuestion;
    }
    cQuestion = (cQuestion || "").toString();
    cDefault = (cDefault || "").toString();
    return this._externalCall("prompt", [cQuestion, cDefault || ""]);
  }

  setInterval(cExpr, nMilliseconds = 0) {
    if (cExpr && typeof cExpr === "object") {
      nMilliseconds = cExpr.nMilliseconds || 0;
      cExpr = cExpr.cExpr;
    }

    if (typeof cExpr !== "string") {
      throw new TypeError("First argument of app.setInterval must be a string");
    }
    if (typeof nMilliseconds !== "number") {
      throw new TypeError(
        "Second argument of app.setInterval must be a number"
      );
    }
    const callbackId = this._registerTimeoutCallback(cExpr);
    this._externalCall("setInterval", [callbackId, nMilliseconds]);
    return this._registerTimeout(callbackId, true);
  }

  setTimeOut(cExpr, nMilliseconds = 0) {
    if (cExpr && typeof cExpr === "object") {
      nMilliseconds = cExpr.nMilliseconds || 0;
      cExpr = cExpr.cExpr;
    }

    if (typeof cExpr !== "string") {
      throw new TypeError("First argument of app.setTimeOut must be a string");
    }
    if (typeof nMilliseconds !== "number") {

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Pass the delay as a number literal: app.setInterval('myFunc()', 1000).
  2. Coerce values from untrusted sources: Number(field.value) before calling.
  3. In object form, ensure nMilliseconds is a number, not a string.

Example fix

// before
app.setInterval('myFunc()', field.value); // string from form
// after
app.setInterval('myFunc()', Number(field.value));
Defensive patterns

Strategy: type-guard

Validate before calling

// Coerce numeric delay before calling app.setInterval.
function scheduleInterval(app, cExpr, nMilliseconds = 0) {
  const ms = Number(nMilliseconds);
  if (!Number.isFinite(ms)) throw new TypeError('nMilliseconds must be a finite number');
  return app.setInterval(cExpr, ms);
}

Type guard

const isFiniteNumber = (v) => typeof v === 'number' && Number.isFinite(v);

Try / catch

try { app.setInterval('fn()', maybeMs); }
catch (e) {
  if (!(e instanceof TypeError && /Second argument of app\.setInterval/.test(e.message))) throw e;
  app.setInterval('fn()', 0); // safe default
}

Prevention

When it happens

Trigger: Calling app.setInterval('myFunc()', '1000') with a string delay, or object form {cExpr:'myFunc()', nMilliseconds:'1000'}. The check at src/scripting_api/app.js:618 fails typeof nMilliseconds !== 'number'.

Common situations: Delay read from a form field or config string and passed uncoerced; default 0 overridden by a non-numeric value; Acrobat JS docs accepting string numbers but PDF.js being stricter.

Related errors


AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13). Data as JSON: /api/errors/fea8a06d0287a202. Report an issue: GitHub.