> ## Documentation Index
> Fetch the complete documentation index at: https://prefab.prefect.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Set Interval

> Run actions on a repeating schedule with optional stop conditions.

export const ComponentPreview = ({json, height, resizable, bare, hideJson, playground, children}) => {
  const hostRef = React.useRef(null);
  const handleRef = React.useRef(null);
  const cardRef = React.useRef(null);
  const containerRef = React.useRef(null);
  const [previewWidth, setPreviewWidth] = React.useState(null);
  const [isDragging, setIsDragging] = React.useState(false);
  const [playgroundHref, setPlaygroundHref] = React.useState(null);
  React.useEffect(function () {
    if (!playground) return;
    var isLocal = location.hostname === "localhost" || location.hostname === "127.0.0.1";
    var path = isLocal ? "/playground" : "/docs/playground";
    var url = new URL(path, window.location.href);
    url.hash = "code=" + playground;
    setPlaygroundHref(url.href);
  }, [playground]);
  var jsonStr = typeof json === "string" ? json : JSON.stringify(json, null, 2);
  React.useEffect(function () {
    if (cardRef.current) {
      var card = cardRef.current.closest(".group");
      if (card) card.classList.remove("group");
    }
    var host = hostRef.current;
    if (!host || !json) return;
    function mount() {
      if (!window.__prefab || !host) return;
      var dark = document.documentElement.classList.contains("dark");
      handleRef.current = window.__prefab.mountPreview(host, jsonStr, {
        dark: dark
      });
      if (bare && host.shadowRoot) {
        var m = host.shadowRoot.querySelector("[data-prefab-mount]");
        if (m) m.style.background = "transparent";
      }
    }
    if (window.__prefab) {
      mount();
    } else {
      if (!window.__prefabLoading) {
        if (!window.__prefabReady) {
          var s = document.createElement("script");
          s.src = "/renderer.js";
          document.head.appendChild(s);
        }
        window.__prefabLoading = new Promise(function (resolve) {
          function check() {
            if (window.__prefabReady) {
              window.__prefabReady.then(resolve);
            } else {
              setTimeout(check, 10);
            }
          }
          check();
        });
      }
      window.__prefabLoading.then(mount);
    }
    var observer = new MutationObserver(function () {
      var dark = document.documentElement.classList.contains("dark");
      if (handleRef.current) handleRef.current.setDark(dark);
    });
    observer.observe(document.documentElement, {
      attributes: true,
      attributeFilter: ["class"]
    });
    return function () {
      observer.disconnect();
      if (handleRef.current) {
        handleRef.current.unmount();
        handleRef.current = null;
      }
    };
  }, [json]);
  function startResize(e) {
    e.preventDefault();
    var startX = e.clientX;
    var startW = hostRef.current ? hostRef.current.offsetWidth : 0;
    var maxW = containerRef.current ? containerRef.current.offsetWidth - 10 : startW;
    setIsDragging(true);
    function onMove(ev) {
      var w = Math.max(260, Math.min(startW + (ev.clientX - startX), maxW));
      setPreviewWidth(w);
    }
    function onUp() {
      document.removeEventListener("mousemove", onMove);
      document.removeEventListener("mouseup", onUp);
      setIsDragging(false);
    }
    document.addEventListener("mousemove", onMove);
    document.addEventListener("mouseup", onUp);
  }
  if (bare) {
    return <div ref={hostRef} />;
  }
  if (resizable) {
    return <Card>
        <div ref={cardRef} />
        <div ref={containerRef} style={{
      display: "flex",
      position: "relative",
      overflow: "hidden"
    }}>
          <div ref={hostRef} style={{
      flex: previewWidth ? "none" : 1,
      width: previewWidth ? previewWidth + "px" : undefined,
      minWidth: 0
    }} />
          <div onMouseDown={startResize} style={{
      width: "14px",
      flexShrink: 0,
      cursor: "col-resize",
      background: isDragging ? "var(--border, #e5e7eb)" : "color-mix(in srgb, var(--border, #e5e7eb) 40%, transparent)",
      display: "flex",
      alignItems: "center",
      justifyContent: "center",
      transition: "background 0.15s",
      userSelect: "none"
    }}>
            <svg width="6" height="24" style={{
      opacity: 0.5
    }}>
              <circle cx="2" cy="4" r="1.2" fill="currentColor" />
              <circle cx="2" cy="9" r="1.2" fill="currentColor" />
              <circle cx="2" cy="14" r="1.2" fill="currentColor" />
              <circle cx="2" cy="19" r="1.2" fill="currentColor" />
              <circle cx="5" cy="4" r="1.2" fill="currentColor" />
              <circle cx="5" cy="9" r="1.2" fill="currentColor" />
              <circle cx="5" cy="14" r="1.2" fill="currentColor" />
              <circle cx="5" cy="19" r="1.2" fill="currentColor" />
            </svg>
          </div>
          <div style={{
      flex: previewWidth ? 1 : "0 0 24px",
      minWidth: "24px",
      background: "repeating-linear-gradient(-45deg, transparent, transparent 3px, var(--border, #e5e7eb) 3px, var(--border, #e5e7eb) 4px)",
      opacity: 0.4
    }} />
          {previewWidth && <div style={{
      position: "absolute",
      bottom: "8px",
      right: "8px",
      background: "var(--background, white)",
      border: "1px solid var(--border, #e5e7eb)",
      borderRadius: "4px",
      padding: "1px 6px",
      fontSize: "11px",
      fontFamily: "monospace",
      color: "var(--muted-foreground, #6b7280)",
      pointerEvents: "none"
    }}>
              {previewWidth}px
            </div>}
        </div>
        {playgroundHref && <div style={{
      display: "flex",
      justifyContent: "flex-end",
      padding: "4px 0 8px"
    }}>
            <a href={playgroundHref} target="_blank" rel="noopener noreferrer" style={{
      fontSize: "11px",
      opacity: 0.4,
      textDecoration: "none",
      textDecorationLine: "none",
      borderBottom: "none",
      boxShadow: "none",
      display: "inline-flex",
      alignItems: "center",
      gap: "3px",
      transition: "opacity 0.15s",
      fontWeight: 400
    }} onMouseOver={function (e) {
      e.currentTarget.style.opacity = "0.6";
    }} onMouseOut={function (e) {
      e.currentTarget.style.opacity = "0.4";
    }}>
              Edit in Playground
              <svg xmlns="http://www.w3.org/2000/svg" width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M15 3h6v6" /><path d="M10 14 21 3" /><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6" /></svg>
            </a>
          </div>}
        <div style={{
      marginBottom: "-2rem"
    }}>{children}</div>
      </Card>;
  }
  return <Card>
      <div ref={cardRef} />
      <div ref={hostRef} />
      {playgroundHref && <div style={{
    display: "flex",
    justifyContent: "flex-end",
    padding: "4px 0 8px"
  }}>
          <a href={playgroundHref} target="_blank" rel="noopener noreferrer" style={{
    fontSize: "11px",
    opacity: 0.4,
    textDecoration: "none",
    textDecorationLine: "none",
    borderBottom: "none",
    boxShadow: "none",
    display: "inline-flex",
    alignItems: "center",
    gap: "3px",
    transition: "opacity 0.15s",
    fontWeight: 400
  }} onMouseOver={function (e) {
    e.currentTarget.style.opacity = "0.6";
  }} onMouseOut={function (e) {
    e.currentTarget.style.opacity = "0.4";
  }}>
            Edit in Playground
            <svg xmlns="http://www.w3.org/2000/svg" width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M15 3h6v6" /><path d="M10 14 21 3" /><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6" /></svg>
          </a>
        </div>}
      <div style={{
    marginBottom: "-2rem"
  }}>{children}</div>
    </Card>;
};

`SetInterval` runs actions on a repeating timer. Each tick fires `on_tick`, and the timer stops automatically when a `while_` condition becomes false, a `count` limit is reached, or both. When it stops, `on_complete` fires one final time.

## One-Shot Delay

Set `count=1` to fire once after a pause — no separate "delay" action needed. Put your logic in `on_complete` and it runs when the timer finishes. The button below swaps to a waiting state so the user knows something is happening.

<ComponentPreview json={{"view":{"type":"Button","label":"{{ waiting ? 'Waiting\u2026' : 'Start 3 second timer' }}","variant":"default","size":"default","disabled":"{{ waiting | default:false }}","onClick":[{"action":"setState","key":"waiting","value":true},{"action":"setInterval","duration":3000,"count":1,"onComplete":[{"action":"showToast","message":"Three seconds. An eternity for a brain the size of a planet.","variant":"info"},{"action":"setState","key":"waiting","value":false}]}]}}} playground="ZnJvbSBwcmVmYWJfdWkuY29tcG9uZW50cyBpbXBvcnQgQnV0dG9uCmZyb20gcHJlZmFiX3VpLmFjdGlvbnMgaW1wb3J0IFNldEludGVydmFsLCBTZXRTdGF0ZSwgU2hvd1RvYXN0CmZyb20gcHJlZmFiX3VpLnJ4IGltcG9ydCBSeAoKd2FpdGluZyA9IFJ4KCJ3YWl0aW5nIikKCkJ1dHRvbigKICAgIHdhaXRpbmcudGhlbigiV2FpdGluZ-KApiIsICJTdGFydCAzIHNlY29uZCB0aW1lciIpLAogICAgZGlzYWJsZWQ9d2FpdGluZy5kZWZhdWx0KEZhbHNlKSwKICAgIG9uX2NsaWNrPVsKICAgICAgICBTZXRTdGF0ZSh3YWl0aW5nLCBUcnVlKSwKICAgICAgICBTZXRJbnRlcnZhbCgKICAgICAgICAgICAgMzAwMCwKICAgICAgICAgICAgY291bnQ9MSwKICAgICAgICAgICAgb25fY29tcGxldGU9WwogICAgICAgICAgICAgICAgU2hvd1RvYXN0KAogICAgICAgICAgICAgICAgICAgICJUaHJlZSBzZWNvbmRzLiBBbiBldGVybml0eSBmb3IiCiAgICAgICAgICAgICAgICAgICAgIiBhIGJyYWluIHRoZSBzaXplIG9mIGEgcGxhbmV0LiIsCiAgICAgICAgICAgICAgICAgICAgdmFyaWFudD0iaW5mbyIsCiAgICAgICAgICAgICAgICApLAogICAgICAgICAgICAgICAgU2V0U3RhdGUod2FpdGluZywgRmFsc2UpLAogICAgICAgICAgICBdLAogICAgICAgICksCiAgICBdLAopCg">
  <CodeGroup>
    ```python Python icon="python" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    from prefab_ui.components import Button
    from prefab_ui.actions import SetInterval, SetState, ShowToast
    from prefab_ui.rx import Rx

    waiting = Rx("waiting")

    Button(
        waiting.then("Waiting…", "Start 3 second timer"),
        disabled=waiting.default(False),
        on_click=[
            SetState(waiting, True),
            SetInterval(
                3000,
                count=1,
                on_complete=[
                    ShowToast(
                        "Three seconds. An eternity for"
                        " a brain the size of a planet.",
                        variant="info",
                    ),
                    SetState(waiting, False),
                ],
            ),
        ],
    )
    ```

    ```json Protocol icon="brackets-curly" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    {
      "view": {
        "type": "Button",
        "label": "{{ waiting ? 'Waiting\u2026' : 'Start 3 second timer' }}",
        "variant": "default",
        "size": "default",
        "disabled": "{{ waiting | default:false }}",
        "onClick": [
          {"action": "setState", "key": "waiting", "value": true},
          {
            "action": "setInterval",
            "duration": 3000,
            "count": 1,
            "onComplete": [
              {
                "action": "showToast",
                "message": "Three seconds. An eternity for a brain the size of a planet.",
                "variant": "info"
              },
              {"action": "setState", "key": "waiting", "value": false}
            ]
          }
        ]
      }
    }
    ```
  </CodeGroup>
</ComponentPreview>

## Countdown

Each tick decrements `seconds` in state, and the display reflects the current value through reactive expressions. When `seconds` reaches zero, the `while_` condition becomes false and the interval stops on its own.

<ComponentPreview json={{"view":{"cssClass":"gap-4 items-center","type":"Column","children":[{"cssClass":"text-2xl font-mono","content":"{{ seconds > 0 ? 'T-minus ' + seconds + '\u2026' : 'Ready for launch' }}","type":"Text"},{"type":"Button","label":"{{ seconds > 0 ? 'Counting down' : 'Begin countdown' }}","variant":"default","size":"default","disabled":"{{ seconds > 0 }}","onClick":[{"action":"setState","key":"seconds","value":10},{"action":"setInterval","duration":1000,"while":"{{ seconds > 0 }}","onTick":{"action":"setState","key":"seconds","value":"{{ seconds - 1 }}"}}]}]}}} playground="ZnJvbSBwcmVmYWJfdWkuY29tcG9uZW50cyBpbXBvcnQgQnV0dG9uLCBDb2x1bW4sIFRleHQKZnJvbSBwcmVmYWJfdWkuYWN0aW9ucyBpbXBvcnQgU2V0SW50ZXJ2YWwsIFNldFN0YXRlCmZyb20gcHJlZmFiX3VpLnJ4IGltcG9ydCBSeAoKc2Vjb25kcyA9IFJ4KCJzZWNvbmRzIikKCndpdGggQ29sdW1uKGdhcD00LCBjc3NfY2xhc3M9Iml0ZW1zLWNlbnRlciIpOgogICAgVGV4dCgKICAgICAgICAoc2Vjb25kcyA-IDApLnRoZW4oCiAgICAgICAgICAgICJULW1pbnVzICIgKyBzZWNvbmRzICsgIuKApiIsCiAgICAgICAgICAgICJSZWFkeSBmb3IgbGF1bmNoIiwKICAgICAgICApLAogICAgICAgIGNzc19jbGFzcz0idGV4dC0yeGwgZm9udC1tb25vIiwKICAgICkKICAgIEJ1dHRvbigKICAgICAgICAoc2Vjb25kcyA-IDApLnRoZW4oIkNvdW50aW5nIGRvd24iLCAiQmVnaW4gY291bnRkb3duIiksCiAgICAgICAgZGlzYWJsZWQ9c2Vjb25kcyA-IDAsCiAgICAgICAgb25fY2xpY2s9WwogICAgICAgICAgICBTZXRTdGF0ZShzZWNvbmRzLCAxMCksCiAgICAgICAgICAgIFNldEludGVydmFsKAogICAgICAgICAgICAgICAgMTAwMCwKICAgICAgICAgICAgICAgIHdoaWxlXz1zZWNvbmRzID4gMCwKICAgICAgICAgICAgICAgIG9uX3RpY2s9U2V0U3RhdGUoc2Vjb25kcywgc2Vjb25kcyAtIDEpLAogICAgICAgICAgICApLAogICAgICAgIF0sCiAgICApCg">
  <CodeGroup>
    ```python Python icon="python" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    from prefab_ui.components import Button, Column, Text
    from prefab_ui.actions import SetInterval, SetState
    from prefab_ui.rx import Rx

    seconds = Rx("seconds")

    with Column(gap=4, css_class="items-center"):
        Text(
            (seconds > 0).then(
                "T-minus " + seconds + "…",
                "Ready for launch",
            ),
            css_class="text-2xl font-mono",
        )
        Button(
            (seconds > 0).then("Counting down", "Begin countdown"),
            disabled=seconds > 0,
            on_click=[
                SetState(seconds, 10),
                SetInterval(
                    1000,
                    while_=seconds > 0,
                    on_tick=SetState(seconds, seconds - 1),
                ),
            ],
        )
    ```

    ```json Protocol icon="brackets-curly" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    {
      "view": {
        "cssClass": "gap-4 items-center",
        "type": "Column",
        "children": [
          {
            "cssClass": "text-2xl font-mono",
            "content": "{{ seconds > 0 ? 'T-minus ' + seconds + '\u2026' : 'Ready for launch' }}",
            "type": "Text"
          },
          {
            "type": "Button",
            "label": "{{ seconds > 0 ? 'Counting down' : 'Begin countdown' }}",
            "variant": "default",
            "size": "default",
            "disabled": "{{ seconds > 0 }}",
            "onClick": [
              {"action": "setState", "key": "seconds", "value": 10},
              {
                "action": "setInterval",
                "duration": 1000,
                "while": "{{ seconds > 0 }}",
                "onTick": {"action": "setState", "key": "seconds", "value": "{{ seconds - 1 }}"}
              }
            ]
          }
        ]
      }
    }
    ```
  </CodeGroup>
</ComponentPreview>

## Start and Stop

Store a boolean flag in state and reference it in `while_`. Any action can flip the flag — the interval checks it on the next tick and stops. Here, one button starts an incrementing counter and another stops it. The same pattern works for periodic server polling with `CallTool` in `on_tick`.

<ComponentPreview json={{"view":{"cssClass":"gap-4 items-center","type":"Row","children":[{"type":"Button","label":"Start","variant":"default","size":"default","disabled":"{{ running | default:false }}","onClick":[{"action":"setState","key":"running","value":true},{"action":"setState","key":"ticks","value":0},{"action":"setInterval","duration":1000,"while":"{{ running }}","onTick":{"action":"setState","key":"ticks","value":"{{ ticks + 1 }}"}}]},{"cssClass":"text-2xl font-mono tabular-nums","content":"{{ ticks | default:0 }}","type":"Text"},{"type":"Button","label":"Stop","variant":"outline","size":"default","disabled":"{{ !running }}","onClick":{"action":"setState","key":"running","value":false}}]}}} playground="ZnJvbSBwcmVmYWJfdWkuY29tcG9uZW50cyBpbXBvcnQgQnV0dG9uLCBSb3csIFRleHQKZnJvbSBwcmVmYWJfdWkuYWN0aW9ucyBpbXBvcnQgU2V0SW50ZXJ2YWwsIFNldFN0YXRlCmZyb20gcHJlZmFiX3VpLnJ4IGltcG9ydCBSeAoKcnVubmluZyA9IFJ4KCJydW5uaW5nIikKdGlja3MgPSBSeCgidGlja3MiKQoKd2l0aCBSb3coZ2FwPTQsIGNzc19jbGFzcz0iaXRlbXMtY2VudGVyIik6CiAgICBCdXR0b24oCiAgICAgICAgIlN0YXJ0IiwKICAgICAgICBkaXNhYmxlZD1ydW5uaW5nLmRlZmF1bHQoRmFsc2UpLAogICAgICAgIG9uX2NsaWNrPVsKICAgICAgICAgICAgU2V0U3RhdGUocnVubmluZywgVHJ1ZSksCiAgICAgICAgICAgIFNldFN0YXRlKHRpY2tzLCAwKSwKICAgICAgICAgICAgU2V0SW50ZXJ2YWwoCiAgICAgICAgICAgICAgICAxMDAwLAogICAgICAgICAgICAgICAgd2hpbGVfPXJ1bm5pbmcsCiAgICAgICAgICAgICAgICBvbl90aWNrPVNldFN0YXRlKHRpY2tzLCB0aWNrcyArIDEpLAogICAgICAgICAgICApLAogICAgICAgIF0sCiAgICApCiAgICBUZXh0KAogICAgICAgIHRpY2tzLmRlZmF1bHQoMCksCiAgICAgICAgY3NzX2NsYXNzPSJ0ZXh0LTJ4bCBmb250LW1vbm8gdGFidWxhci1udW1zIiwKICAgICkKICAgIEJ1dHRvbigKICAgICAgICAiU3RvcCIsCiAgICAgICAgdmFyaWFudD0ib3V0bGluZSIsCiAgICAgICAgZGlzYWJsZWQ9fnJ1bm5pbmcsCiAgICAgICAgb25fY2xpY2s9U2V0U3RhdGUocnVubmluZywgRmFsc2UpLAogICAgKQo">
  <CodeGroup>
    ```python Python icon="python" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    from prefab_ui.components import Button, Row, Text
    from prefab_ui.actions import SetInterval, SetState
    from prefab_ui.rx import Rx

    running = Rx("running")
    ticks = Rx("ticks")

    with Row(gap=4, css_class="items-center"):
        Button(
            "Start",
            disabled=running.default(False),
            on_click=[
                SetState(running, True),
                SetState(ticks, 0),
                SetInterval(
                    1000,
                    while_=running,
                    on_tick=SetState(ticks, ticks + 1),
                ),
            ],
        )
        Text(
            ticks.default(0),
            css_class="text-2xl font-mono tabular-nums",
        )
        Button(
            "Stop",
            variant="outline",
            disabled=~running,
            on_click=SetState(running, False),
        )
    ```

    ```json Protocol icon="brackets-curly" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    {
      "view": {
        "cssClass": "gap-4 items-center",
        "type": "Row",
        "children": [
          {
            "type": "Button",
            "label": "Start",
            "variant": "default",
            "size": "default",
            "disabled": "{{ running | default:false }}",
            "onClick": [
              {"action": "setState", "key": "running", "value": true},
              {"action": "setState", "key": "ticks", "value": 0},
              {
                "action": "setInterval",
                "duration": 1000,
                "while": "{{ running }}",
                "onTick": {"action": "setState", "key": "ticks", "value": "{{ ticks + 1 }}"}
              }
            ]
          },
          {
            "cssClass": "text-2xl font-mono tabular-nums",
            "content": "{{ ticks | default:0 }}",
            "type": "Text"
          },
          {
            "type": "Button",
            "label": "Stop",
            "variant": "outline",
            "size": "default",
            "disabled": "{{ !running }}",
            "onClick": {"action": "setState", "key": "running", "value": false}
          }
        ]
      }
    }
    ```
  </CodeGroup>
</ComponentPreview>

If neither `while_` nor `count` is set, the interval runs indefinitely until the component tree is replaced.

## API Reference

<Card icon="code" title="SetInterval Parameters">
  <ParamField body="duration" type="int" required>
    Milliseconds between ticks. Passed as a positional argument.
  </ParamField>

  <ParamField body="while_" type="str | None" default="None">
    Condition expression re-evaluated each tick against current state. When
    it evaluates to falsy, the interval stops and `on_complete` fires. Serializes
    as `"while"` on the wire.
  </ParamField>

  <ParamField body="count" type="int | None" default="None">
    Maximum number of ticks. The interval stops after this many.
  </ParamField>

  <ParamField body="on_tick" type="Action | list[Action] | None" default="None">
    Action(s) to run each tick. `$event` is the tick number (1, 2, 3, ...).
  </ParamField>

  <ParamField body="on_complete" type="Action | list[Action] | None" default="None">
    Action(s) to run when the interval finishes.
  </ParamField>
</Card>

## Protocol Reference

```json SetInterval theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
{
  "action": "setInterval",
  "duration": "number | string (required)",
  "while?": "string",
  "count?": "number",
  "onTick?": "Action | Action[]",
  "onComplete?": "Action | Action[]"
}
```

For the complete protocol schema, see [SetInterval](/docs/protocol/set-interval).
