Reference · Browser
every
every(num, fn, map | null=) -> nullWhat 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
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>}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
- For once, later, run_after: there is no
setTimeout. A debounce or a throttle is aneveryunder a state flag that the tick turns off, as above. - It is called from the component's code, a
letor anifthere, or from one of its event handlers or its@mount: started there, it runs until the component leaves the page. Started after a handler's first wait (aserver fn's answer), it belongs to no one, and is an error in the console (`every` works in a component). - What a statement starts, it stops: before the statement runs again, and when its component, row or branch leaves the page, the timer is cleared.
- A tick's function may call a
server fnornotify; an error in it is written to the console and the next tick still runs. A tab in the background is slowed by the browser to once a second at most. Under one millisecond is taken as one. - While the server renders the component it does nothing and gives
null; the browser starts the timer when it resumes the component, which loads its bundle as soon as the page is idle, since it runs by itself. - At the top of a program it is a server tick: counted from 1970 so each tick falls on
a multiple of
ms; a tick still running when the next is due skips it; ticks stop with the server and on a reload; a program with one is a server. Two ticks cannot share a name, and a name is letters, digits,_,-and..quillo checkrefuses a name twice only where both calls surely run, and warns where both may (two arms of anif, a loop); the start refuses the second only if it really comes. - On the server, only as the program starts: called in a route, a
server fn, a job or a tick, it is a runtime error (a 500 in a route), where the tick was said to be added and never ran.quillo checkreads its written-out arguments as the start does. quillo checkwants a number and a function:every("fast", f)isargument 1 of `every` must be num, got strandevery(100)ismissing argument `f`, a function, in call to `every`: every(ms, f, options).
fn tick() { print("tick") }every(50, tick)every(100, tick)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.
fn tick() { print("tick") }print(every(0, tick))line 2: `every` on the server takes at least 1 millisecond: every(50, step)print(every(50, "tick"))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:
if polling { every(5000, () => { status = check_status(id) }) }See also
run_after, listen, live, schedule, sleep, now
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