A countdown from data
When a round ends at a fixed time, the world can put the end time in data once and your view can count down on the clock of the device. The countdown then updates four times a second without any message between the world and your view. The round in the example ends ten seconds after the page opens.
import { useEffect, useState } from 'react';
import { useWorld } from './useWorld.js';
// This fake quiz round ends 10 seconds after it starts.
// The world says once when the round ends (Unix ms) and leaves the countdown to the view.
const quiz = {
name: 'Quiz night',
now: { text: 'Round 1: answer before the time is up.' },
data: { roundEndsAt: Date.now() + 10000 },
};
// Turns 65000 ms into "1:05".
function minutesAndSeconds(ms) {
const seconds = Math.ceil(ms / 1000);
return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, '0')}`;
}
export default function App() {
// While building: the fake world. In the webapp: useWorld()
const { world, error } = useWorld({ fake: { world: quiz } });
const [clock, setClock] = useState(Date.now());
// Four readings a second of the person's clock are enough for a smooth countdown.
useEffect(() => {
const timer = setInterval(() => setClock(Date.now()), 250);
return () => clearInterval(timer);
}, []);
if (error) return <p>Could not connect ({error.code}).</p>;
if (!world) return <p>Connecting…</p>;
const endsAt = world.data.roundEndsAt;
let countdown = 'Waiting for the round…';
if (endsAt !== undefined) countdown = endsAt > clock ? `Time left: ${minutesAndSeconds(endsAt - clock)}` : 'Time is up!';
return (
<>
<h2>{world.name}</h2>
<p>{world.now?.text}</p>
<p style={{ fontSize: 32 }}>{countdown}</p>
</>
);
}import { connectToHost } from '@unaiverse/gui-kit';
// This fake quiz round ends 10 seconds after it starts.
// The world says once when the round ends (Unix ms) and leaves the countdown to the view.
const quiz = {
name: 'Quiz night',
now: { text: 'Round 1: answer before the time is up.' },
data: { roundEndsAt: Date.now() + 10000 },
};
// Turns 65000 ms into "1:05".
function minutesAndSeconds(ms) {
const seconds = Math.ceil(ms / 1000);
return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, '0')}`;
}
// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: quiz } });
const title = document.createElement('h2');
const now = document.createElement('p');
const countdown = document.createElement('p');
countdown.style.fontSize = '32px';
document.body.append(title, now, countdown);
function drawCountdown() {
const endsAt = world.get().data.roundEndsAt;
const clock = Date.now();
if (endsAt === undefined) countdown.textContent = 'Waiting for the round…';
else countdown.textContent = endsAt > clock ? `Time left: ${minutesAndSeconds(endsAt - clock)}` : 'Time is up!';
}
world.subscribe((node) => {
title.textContent = node.name;
now.textContent = node.now?.text ?? '';
drawCountdown(); // draws a new end time at once
});
// Four readings a second of the person's clock are enough for a smooth countdown.
setInterval(drawCountdown, 250);How it works
data.roundEndsAt is a time in milliseconds since the epoch (the unit of Date.now()) and the time left is roundEndsAt - Date.now(). A timer of the view fires every 250 ms. The React version updates a clock state to trigger a re-render and the plain JavaScript version calls drawCountdown. A new end time for a new round makes subscribe run and the countdown follows it at once.
The clock of the device can be off by a few seconds. For a round that must end exactly, the world states when the round ended (in now or in data) and the countdown is only an indication.
See also
- Timer: a countdown component.
- A fake world that changes over time.
- The data of the world.