mozilla/pdf.js · error · TypeError

First argument of app.setInterval must be a string

Error message

First argument of app.setInterval must be a string

What it means

app.setInterval schedules a PDF-JavaScript expression for repeated evaluation after nMilliseconds. PDF.js requires the first argument to be the code string (or an object literal {cExpr, nMilliseconds}); if cExpr is not a string after coercion, a TypeError is thrown. This guards the sandbox from evaluating non-string input.

Source

Thrown at src/scripting_api/app.js:615

    this._document.obj._userActivation = false;

    if (cQuestion && typeof cQuestion === "object") {
      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") {

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Pass the callback body as a string: app.setInterval('myFunc()', 1000).
  2. If using object form, ensure {cExpr: 'myFunc()', nMilliseconds: 1000} has a string cExpr.
  3. For a real JS function, inline its body as a string only if you must; otherwise use the host viewer's native scheduling.

Example fix

// before
app.setInterval(myFunc, 1000); // myFunc is a function
// after
app.setInterval('myFunc()', 1000); // Acrobat JS expects a code string
Defensive patterns

Strategy: type-guard

Validate before calling

// Validate cExpr is a string before calling app.setInterval.
function scheduleInterval(app, cExpr, nMilliseconds = 0) {
  if (typeof cExpr !== 'string' || cExpr.length === 0) {
    throw new TypeError('cExpr must be a non-empty string');
  }
  return app.setInterval(cExpr, nMilliseconds);
}

Type guard

const isCodeString = (v) => typeof v === 'string' && v.trim().length > 0;

Try / catch

try { app.setInterval(maybeExpr, 1000); }
catch (e) {
  if (!(e instanceof TypeError && /First argument of app\.setInterval/.test(e.message))) throw e;
  // fall back to no-op or host scheduler
}

Prevention

When it happens

Trigger: Calling app.setInterval(someNumber, 1000), app.setInterval(null, 1000) after the object-form unwrap, or app.setInterval({cExpr: 42}) where cExpr is not a string. The check at src/scripting_api/app.js:615 fails typeof cExpr !== 'string'.

Common situations: Passing a function reference instead of a code string (common from JS authors used to window.setInterval); passing undefined when the expression is optional; object-form call missing cExpr.

Related errors


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