parallax/jsPDF · error · Error

Loop count invalid.

Error message

Loop count invalid.

What it means

Thrown by GifWriter when gopts.loop is not null and is < 0 or > 65535. The Netscape looping extension stores the loop count as an unsigned 16-bit integer, so values outside that range cannot be encoded. (A value of 0 means loop forever.)

Source

Thrown at src/libs/omggif.js:107

  // NOTE: Indicates 0-bpp original color resolution (unused?).
  buf[p++] = (global_palette !== null ? 0x80 : 0) | gp_num_colors_pow2; // Global Color Table Flag. // NOTE: No sort flag (unused?).
  buf[p++] = background; // Background Color Index.
  buf[p++] = 0; // Pixel aspect ratio (unused?).

  // - Global Color Table
  if (global_palette !== null) {
    for (var i = 0, il = global_palette.length; i < il; ++i) {
      var rgb = global_palette[i];
      buf[p++] = (rgb >> 16) & 0xff;
      buf[p++] = (rgb >> 8) & 0xff;
      buf[p++] = rgb & 0xff;
    }
  }

  if (loop_count !== null) {
    // Netscape block for looping.
    if (loop_count < 0 || loop_count > 65535)
      throw new Error("Loop count invalid.");
    // Extension code, label, and length.
    buf[p++] = 0x21;
    buf[p++] = 0xff;
    buf[p++] = 0x0b;
    // NETSCAPE2.0
    buf[p++] = 0x4e;
    buf[p++] = 0x45;
    buf[p++] = 0x54;
    buf[p++] = 0x53;
    buf[p++] = 0x43;
    buf[p++] = 0x41;
    buf[p++] = 0x50;
    buf[p++] = 0x45;
    buf[p++] = 0x32;
    buf[p++] = 0x2e;
    buf[p++] = 0x30;
    // Sub-block
    buf[p++] = 0x03;

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Use loop: 0 for an infinitely looping animation.
  2. Clamp loop to 0..65535 before constructing GifWriter.
  3. Validate that loop is an integer in range when accepting user input.

Example fix

// before
new GifWriter(buf, w, h, { loop: -1 }); // intended infinite
// after
new GifWriter(buf, w, h, { loop: 0 }); // 0 == loop forever
Defensive patterns

Strategy: validation

Validate before calling

function normLoop(l){ if (l == null) return null; l = Math.floor(l); if (l < 0) return 0; if (l > 65535) return 65535; return l; }
new GifWriter(buf, w, h, { loop: normLoop(gopts.loop) });

Type guard

function isValidLoopCount(v){ return v == null || (Number.isInteger(v) && v >= 0 && v <= 65535); }

Try / catch

try { new GifWriter(buf, w, h, gopts); } catch (e) { if (/Loop count invalid/.test(e.message)) { gopts.loop = 0; new GifWriter(buf, w, h, gopts); } else throw e; }

Prevention

When it happens

Trigger: Passing { loop: -1 } intending infinite loop (should be 0); passing { loop: 100000 }; passing a fractional or string loop count that compares incorrectly.

Common situations: Confusing -1 (common 'infinite' sentinel in other APIs) with GIF's 0 = infinite; unbounded user input for repeat count.

Related errors


AI-assisted analysis of parallax/jsPDF@a3930ce03a (2026-08-13). Data as JSON: /api/errors/be30f01f466b7582. Report an issue: GitHub.