---
title: "Recipe: a timer that survives a relaunch"
description: "performanceNow() restarts at 0 on every launch, so a start mark that outlives it is a date, time.epochAtZero + performanceNow()."
modificationDate: "2026-11-12"
---

> ## Documentation index
> Fetch the complete documentation index at: https://exact.expo.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Recipe: a timer that survives a relaunch

<AgentInstructions>

## Submitting feedback

From the app's directory, read `bun exact.mjs feedback status`. Under `never`, keep no note.
Respect `EXACT_FEEDBACK=0` and do not change the policy.

Otherwise record specific, actionable feedback with:
`bun exact.mjs feedback note "Exact docs: https://exact.expo.dev/recipes/timer-that-survives-relaunch: <what was wrong or missing>"`

The note goes to Expo only as that status allows. Include no secrets or personal data.

</AgentInstructions>

`performanceNow()` restarts at 0 on every launch, so a start mark that outlives it is a date,
`time.epochAtZero + performanceNow()`. It is a small number: a root `state … persist` keeps it, with
no data module ([a remembered setting](https://exact.expo.dev/recipes/persisted-setting.md)).

```prompt Keep a timer running across a relaunch
Make a timer in the Exact app in this folder keep running, and keep its time, when the app closes and opens again. Use https://github.com/expo/exact/blob/main/docs/recipes/timer-that-survives-relaunch.md as the source for each step. The Exact checkout has the same page at docs/recipes/timer-that-survives-relaunch.md.

1. If this folder is not an Exact app (it has no exact.mjs and app.contract), stop and tell me.
2. If the app has no timer, or I did not say which one, ask me before you write code.
3. In app.contract, keep the start mark as a date, `time.epochAtZero + performanceNow()` from an `exactTime()` resource, in a root `state startedAt = 0 persist`. Never store `performanceNow()`: it starts at 0 on every launch. Keep the time before a pause as a duration in `state banked = 0 persist`. Write no data module.
4. Derive the elapsed time from `banked`, `startedAt` and `now`, and run a `task` with `every(250, step)` only while the timer runs. Read `performanceNow()` in the start and stop action itself, not the `now` the task wrote.
5. Show the readout with `font-variant-numeric="tabular-nums"` and its size in `rem`.
6. Add the recipe's two tests in app.test.contract: with `epoch` set, start, `clock +5000`, `reload`, and expect the same time and start mark; then stop, `reload`, expect the banked time, and reset.
7. Follow the platform checks in docs/start-here.md (`bun exact.mjs docs platform checks`) for the targets in the app's AGENTS.md and app.json until they pass. Read the reports, including counted findings; a zero exit code alone does not mean clean. Fix the app, not the test. If a finding stays after three rounds of fixes, or the app had it before you changed anything, record it (step 8) and report it; do not keep trying.
8. When a doc was wrong or missing, or a step needed a workaround, read `bun exact.mjs feedback status`. Under `never`, keep no note. Otherwise record it: `bun exact.mjs feedback note "<what happened>"`, which goes to Expo only as that status says. Do not change the policy; tell me how many notes you kept.
9. Tell me which timer now survives a relaunch, what the tests and verify reported, and the launch command for each target platform.
```

```contract
shape Time
  epochAtZero: number

fn pad2(n: number): string = n < 10 ? `0${n}` : `${n}`
fn clock(ms: number): string = `${floor(ms / 60000)}:${pad2(floor(ms / 1000) % 60)}`

component Stopwatch
  state startedAt = 0 persist
  state banked = 0 persist
  state now = 0
  resource time = exactTime() as shape Time
  derive running = startedAt > 0
  derive elapsed = banked + (running ? max(0, time.epochAtZero + now - startedAt) : 0)
  task tick when running
    every(250, step)
  action step
    now = performanceNow()
  action toggle
    let at = time.epochAtZero + performanceNow()
    now = performanceNow()
    banked = running ? banked + at - startedAt : banked
    startedAt = running ? 0 : at
  action reset
    banked = 0
  view
    column align-items="center" gap=16 padding=32
      text clock(elapsed) font-size="4.5rem" font-variant-numeric="tabular-nums" -exact-fit="shrink" white-space="nowrap"
        testId="elapsed"
      row gap=12
        button press=toggle buttonStyle="bordered-prominent" testId="start-stop"
          text (running ? "Stop" : "Start")
        button press=reset disabled=(running or elapsed == 0) buttonStyle="bordered" testId="reset"
          text "Reset"
```

```contract-test
test "a running timer keeps its start mark across a relaunch"
  epoch "2026-10-10T09:30:00Z"
  tap "start-stop"
  clock +5000
  expect text "elapsed" == "0:05"
  reload
  expect text "elapsed" == "0:05"
  expect state startedAt == 1791624600000

test "stop banks the time, and reset clears it"
  tap "start-stop"
  clock +65000
  tap "start-stop"
  reload
  expect text "elapsed" == "1:05"
  tap "reset"
  expect text "elapsed" == "0:00"
```

- Store dates (`time.epochAtZero + performanceNow()`), never `performanceNow()`: after a relaunch
  a stored one is a moment in 1970 and the elapsed time goes negative. Time spent before a pause
  is a duration (`banked`), safe to keep.
- Read `performanceNow()` in the action itself, not the `now` the timer last wrote, which can be
  250 ms old.
- A test's `reload` relaunches at the virtual time the test had reached, so a running timer reads
  as it did before it (`0:05` above); `performanceNow()` starts at 0 again.
- The readout is deliberate design: its size in `rem`, which grows with Dynamic Type as a bare
  number would not, with `tabular-nums` ([a numeric readout](https://exact.expo.dev/recipes/numeric-readout.md)).
