← how-to

timer

zsx / 7guis

program
-- 7GUIs task 4 — Timer.
--
-- The tick is an event: the platform fires `@tick` at frame
-- cadence with the elapsed milliseconds as payload, through the
-- same queue user events use — competing user/timer interactions
-- serialize by construction. Stopping IS the clamp: once
-- `elapsed` reaches `duration` the handler adds nothing, and
-- raising the duration past `elapsed` resumes ticking.
-- @cmd — zo run 004_timer.zo

load core::zsx::*;
load core::math::round;

fun one_decimal(x: float) -> float {
  round(x * 10.0) / 10.0
}

fun main() {
  mut elapsed: float = 0.0;
  mut duration: float = 15.0;
  mut shown: str = "0";

  imu timer: </> ::= <div @tick={fn(e) => {
    match e.value.parse_float() {
      Option::Some(dt_ms) => {
        if elapsed < duration {
          mut next: float = elapsed + dt_ms / 1000.0;

          if next > duration {
            next = duration;
          }

          elapsed = next;
          shown = one_decimal(elapsed).to_str();
        }
      }
      Option::None => {}
    };
  }}>
    <span>Elapsed time:</span>
    <progress value={elapsed} max={duration} />
    <p>{shown}s</p>
    <span>Duration:</span>
    <input
      type="range"
      min="0"
      max="60"
      step="0.1"
      value={duration}
      @input={fn(e) => {
        match e.value.parse_float() {
          Option::Some(next) => duration = next,
          Option::None => {}
        };
      }}
    />
    <button @click={fn() => {
      elapsed = 0.0;
      shown = "0";
    }}>Reset</button>
  </div>;

  #render timer;
}

This program declares a gauge G for the elapsed time e, a label showing e numerically, a slider S adjusting the duration d while the timer runs (reflected immediately, not on release), and a reset button R. When e >= d the timer stops and G is full; raising d past e restarts the ticking. R resets e to zero.

source — 7GUIs (Timer)

reachout

echo -n 'dGhlQGNvbXBpbG9yZHMuaG91c2U=' | base64 --decode

For humans: faq.

For Ai agents: llms.txt (curated index) and llms-full.txt (full docs).

Privacy: No cookies, no ads, no tracking. It's like you were never here.