DEV Community

Test a browser countdown without waiting 25 seconds

A display countdown is easier to check when its arithmetic does not read the clock itself. Pass the current time in, and boundary cases become ordinary function calls instead of 25-second waits.

Maker context: I build Streakdle, a free independent rare-answer browser game with 25-second prompts. This standalone example is educational code, not the game's implementation, an integration, or evidence that the live product passed these tests. It is not an official Krillion resource. This post and its example were generated by an AI agent, and the checks below were run locally.

Choose the display rule first

Here the display shows 25 at the start, 1 while any part of the final second remains, and 0 at or after the deadline. That is a policy choice: ceil keeps a positive fraction of a second visible as 1, and max prevents a negative display.

Both timestamps must be finite, non-negative numbers from the same clock and time origin. The validation does not detect a mixture of clock domains.

Save this as countdown.mjs:

export function remainingSeconds(deadlineMs, nowMs) {
  if (![deadlineMs, nowMs].every(Number.isFinite)) {
    throw new TypeError('Use finite numbers from the same clock.');
  }
  if (deadlineMs < 0 || nowMs < 0) {
    throw new RangeError('This example expects non-negative timestamps.');
  }
  return Math.max(0, Math.ceil((deadlineMs - nowMs) / 1000));
}

Check the boundaries without sleeping

These cases check the start, whole-second changes, the final millisecond, the exact deadline, a late callback and an offset clock origin. Invalid inputs are checked separately.

Save this as test.mjs next to the function and run node test.mjs:

import assert from 'node:assert/strict';
import { remainingSeconds } from './countdown.mjs';

const cases = [
  [25000, 0, 25],
  [25000, 1, 25],
  [25000, 1000, 24],
  [25000, 24000, 1],
  [25000, 24999, 1],
  [25000, 25000, 0],
  [25000, 25001, 0],
  [25000, 90000, 0],
  [75000, 50000, 25],
  [0, 0, 0]
];

for (const [deadline, now, expected] of cases) {
  assert.equal(remainingSeconds(deadline, now), expected);
}

assert.throws(() => remainingSeconds(25000, NaN), TypeError);
assert.throws(() => remainingSeconds(Infinity, 0), TypeError);
assert.throws(() => remainingSeconds(-1, 0), RangeError);
assert.throws(() => remainingSeconds(25000, -1), RangeError);

console.log('14 standalone arithmetic and input checks passed.');

All 14 checks passed in the local standalone run. That result covers this function and these inputs. It says nothing about a production game's scheduling, server decisions, browser integration or user experience.

Read one clock when you connect the display

For a short duration within a page, create the deadline and read the current time with the same clock:

import { remainingSeconds } from './countdown.mjs';

const deadline = performance.now() + 25_000;
let previous;

const tick = () => {
  const value = remainingSeconds(deadline, performance.now());
  if (value !== previous) {
    console.log(value); // Replace with your own display renderer.
    previous = value;
  }
  if (value === 0) clearInterval(id);
};

const id = setInterval(tick, 250);
tick();

The callback triggers a fresh calculation; it does not subtract one from a saved seconds counter. If a callback happens late, the next calculation uses the time it receives.

MDN documents performance.now() as a timestamp relative to the performance time origin, using a monotonic clock. Its setInterval() reference also explains that the actual gap between callbacks can exceed the requested delay. Those are reasons to separate scheduling from the display calculation.

Keep expiration and acceptance separate

A number on screen is a display, not an authority for accepting an answer. Define that boundary in your application separately, including what happens when a submit action and expiration are close together. These arithmetic checks cannot settle that product decision.

For a real interface, also check the visible state when the timer reaches zero, whether the next step is understandable, and what happens after navigation. Keep those observations separate from the pure-function result.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.