Reference · Browser

every

signature
every(num, fn, map | null=) -> null

What it does

In a component's code, calls f every ms milliseconds in the browser, for as long as the code that called it stands: if running { every(100, step) } runs while running is true, starts over when speed or anything else it read changes, and stops when the condition is false or the component leaves the page. It gives null. A tick that finds that timer's last run still waiting does nothing, so a poll slower than its timer runs one call at a time and shows each answer; a run with no answer after 30 seconds (or two gaps, if longer) is given up on, and the next tick starts anew. A clock's tick should not share an every with a call that waits: give it its own. At the top of a program it is the server's clock instead: a tick that runs f every ms milliseconds, named after its function or by { name: "..." }.

Examples

in a program
component Clock() {    state running = false    state speed = 1000    state ticks = 0    if running { every(speed, () => { ticks += 1 }) }    // stops when running is false,    <div>                                                 // starts over when speed changes        <button @click={() => { running = !running }}>{running ? "Stop" : "Start"}</button>        <button @click={() => { speed = speed / 2 }}>Faster</button>        <span>{ticks}</span>    </div>}
in a program
component Saved() {    state shown = false    if shown { every(3000, () => { shown = false }) }     // a one-off delay: the tick turns itself off    <div>        <button @click={() => { shown = true }}>Save</button>        {shown && <span>Saved</span>}    </div>}

Edge cases

this one fails
fn tick() { print("tick") }every(50, tick)every(100, tick)
output
line 3: `every`: there is already a tick named "tick"; give this one another with { name: ... }

Errors

On the server a tick needs at least one millisecond and a function: anything else is a runtime error, not an error value, as the program loads; so is a call once the program has started.

this one fails
fn tick() { print("tick") }print(every(0, tick))
output
line 2: `every` on the server takes at least 1 millisecond: every(50, step)
this one fails
print(every(50, "tick"))
output
line 1: `every` takes milliseconds and a function: every(100, step)

Chaining

every is a statement, not a value: it is not piped into or out of. The function it is given is where the chaining happens:

in a program
if polling { every(5000, () => { status = check_status(id) }) }

See also

run_after, listen, live, schedule, sleep, now

Thread
11Useful? Like it, or add to its thread below.

Thread 0 posts

On every(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo