---
title: "Recipe: timers"
description: "A task is the only timer: every(ms, action) repeats, after(ms, action) fires once, and when cond arms it while it holds."
modificationDate: "2026-11-17"
---

> ## 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: timers

<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/timers: <what was wrong or missing>"`

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

</AgentInstructions>

A `task`, in the root or [a child at a bounded use](https://exact.expo.dev/recipes/timer-in-a-component.md), is the only timer: `every(ms,
action)` repeats, `after(ms, action)` fires once, `when cond` arms it while it holds, so
start and stop are a state. A countdown, its readout and button in the list's first row:

```prompt Add a timer to my app
Add a timer to the Exact app in this folder: work that repeats, fires once, or counts down to an end. Use https://github.com/expo/exact/blob/main/docs/recipes/timers.md as the source for each step. The Exact checkout has the same page at docs/recipes/timers.md.

1. If this folder is not an Exact app (it has no exact.mjs and app.contract), stop and tell me.
2. If I did not say what the timer does, what starts and stops it, and which screen shows it, propose it and ask me before you write code. If the timer must keep its time when the app quits, tell me and use docs/recipes/timer-that-survives-relaunch.md.
3. In app.contract, declare a `task`: it is the only timer. Put it in the root, or in a child at a bounded use as docs/recipes/timer-in-a-component.md describes. Inside it, `every` repeats (the recipe's `every(250, step)`) and `after(ms, action)` fires once.
4. Make start and stop a state: `when cond` arms the task while the condition holds. `key=x` restarts a task when `x` changes.
5. The task's action writes `performanceNow()` into a state: a derive does not update as time passes. Compute the time left from the end (`endsAt - now`). Do not count ticks: the count drifts.
6. Show a readout that changes in place with `font-variant-numeric="tabular-nums"`. For work that can wait, add the recipe's options: `wait=scrollend` with `max=1000`, `wait=idle`, `wait=frame`, `wait=update`, or `tolerance=500` to coalesce wakes.
7. Add a test in app.test.contract like the recipe's. A test's `clock +N` fires the timers it passes: start the timer, move the clock, expect the readout, move the clock past the end, and expect the end state.
8. 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 9) and report it; do not keep trying.
9. 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.
10. Tell me what the timer does and which state starts and stops it, what the tests and checks reported, and the launch command for each target platform. `performanceNow()` restarts at launch, so tell me that this timer does not survive a quit.
```

```contract
routes nav
  home "/"

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

component Tea
  state minutes = 3
  state endsAt = 0
  state now = 0
  state finished = false
  derive running = endsAt > 0
  derive left = running ? max(0, endsAt - now) : finished ? 0 : minutes * 60000
  task tick when running
    every(250, step)
  action step
    now = performanceNow()
    if performanceNow() >= endsAt
      endsAt = 0
      finished = true
  action toggle
    now = performanceNow()
    endsAt = running ? 0 : performanceNow() + minutes * 60000
    finished = false
  action choose(m: number)
    minutes = m
    finished = false
  view
    main navigationKey=`${top(nav).id}` testId="app" width="100%" height="100%"
      for e in stack(nav) key=e.id
        column navigationKey=`${e.id}` navigationScroll="presets" position="absolute" inset=0
          display="flex" flex-direction="column"
          header display="flex" align-items="center" padding="8px 16px"
            text "Tea" role="heading" aria-level=1
          list id="presets" appearance="auto" listStyle="inset-grouped" flex=1 min-height=0
            section
              column align-items="center" gap=12 padding=16
                text mmss(left) font-size="4.5rem" font-variant-numeric="tabular-nums" testId="left"
                text (finished ? "Ready" : running ? "Steeping" : " ") testId="status"
                button press=toggle buttonStyle="bordered-prominent" testId="start-stop"
                  text (running ? "Stop" : "Start")
            section
              for m in [1, 3, 5, 10] key=m
                button press=choose(m) disabled=running testId=`min-${m}`
                  text `${m} min`
                  if minutes == m
                    image "symbol:checkmark"
```

```contract-test
test "a countdown ends at zero"
  tap "min-1"
  tap "start-stop"
  clock +30000
  expect text "left" == "0:30"
  clock +31000
  expect text "left" == "0:00"
  expect text "status" == "Ready"
```

- A task writes `performanceNow()` into state; a derive does not update as time passes. Compute from the end; counting ticks drifts.
- `key=x` restarts a task when `x` changes. A test's `clock +N` fires the timers it passes.
- `performanceNow()` restarts at launch. Store `time.epochAtZero + performanceNow()` for one that survives a quit ([recipe](https://exact.expo.dev/recipes/timer-that-survives-relaunch.md)).

Deferrable work: `every(5000, tick, wait=scrollend, max=1000)` or `after(800, save, wait=idle)`. `wait=frame` joins the next frame; `wait=update` joins existing frame demand. To coalesce wakes, `tolerance=500` instead of `wait=`. [Options](https://exact.expo.dev/reference/developer/timer-options.md).
