# Timer

- **Section:** Components
- **URL:** https://refast.fastapicloud.dev/docs/components/timer
- **Markdown:** https://refast.fastapicloud.dev/docs/components/timer.md
- **Source:** https://github.com/idling-mind/refast-docs/blob/main/docs/pages/components/utility/timer.py
- **Live examples:** the widgets on this page need a browser; open the URL above to try them.
- **Applies to:** refast 0.12.2

An invisible component that repeatedly fires a server callback at a fixed
interval.  It renders nothing in the DOM — place it anywhere in the component
tree and it will keep ticking as long as `enabled=True`.

```python
from refast.components import Timer

Timer(
    interval=2000,        # fire every 2 seconds
    enabled=True,
    on_tick=ctx.callback(handle_tick),
)

async def handle_tick(ctx):
    data = await fetch_latest()
    ctx.state.set("data", data)
    await ctx.refresh()
```

Toggle the timer on and off by updating the `enabled` prop from a callback or
state change — no need to add/remove the component from the tree.

```python
from refast.components import Timer

Timer(
    interval=1000,
    enabled=True,
    on_tick=ctx.callback(handle_tick),
)

async def handle_tick(ctx):
    # called every tick
    await ctx.refresh()
```

## Props

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `interval` | `int` | `1000` | Milliseconds between ticks. Must be ≥ 100 |
| `enabled` | `bool` | `True` | When `False` the timer is paused; no callbacks are fired |
| `on_tick` | `Callback \| None` | `None` | Server callback invoked on each tick |
| `id` | `str \| None` | `None` | Unique element ID |
| `extra_props` | `dict \| None` | `None` | Additional props forwarded to the component |

## Common Patterns

### Live Dashboard

Poll a data source periodically and refresh the UI:

```python
async def refresh_stats(ctx):
    stats = await fetch_stats()
    ctx.state.set("stats", stats)
    await ctx.refresh()

# In the page render:
Timer(interval=5000, on_tick=ctx.callback(refresh_stats))
```

### Toggle On / Off

Bind `enabled` to a state value controlled by a button:

```python
async def toggle_timer(ctx):
    ctx.state.set("running", not ctx.state.get("running", False))
    await ctx.refresh()

running = ctx.state.get("running", False)

Timer(interval=1000, enabled=running, on_tick=ctx.callback(handle_tick))
Button("Start" if not running else "Stop", on_click=ctx.callback(toggle_timer))
```

### One-Shot Countdown

Disable the timer inside its own callback after a fixed number of ticks:

```python
async def countdown_tick(ctx):
    remaining = ctx.state.get("remaining", 10) - 1
    ctx.state.set("remaining", remaining)
    if remaining <= 0:
        ctx.state.set("timer_enabled", False)
    await ctx.refresh()
```

---

Part of the Refast documentation: [all pages](https://refast.fastapicloud.dev/llms.txt), [everything on one page](https://refast.fastapicloud.dev/llms-full.txt), [search](https://refast.fastapicloud.dev/api/search?q=save_prop).
