INPUT /  OUTPUT /  LANGUAGE





timer tag

Persistent logic tag

Carries out code repeatedly, with a time delay between each repetition.

Sample usage

Fields

Primary field

Scalar field

The count. While onTimer is carried out, it holds the number of the current repetition; between repetitions, the number of the next one. Can be given a starting value, which defaults to 1.

Secondary fields

Behaviour

A timer works like a for loop that waits between repetitions. The count starts at the value in the primary field (1 if none is given), and each time the period ends, the timer carries out its onTimer code and the count goes up by one, up to and including limit. Unlike a for loop, the console stays responsive between repetitions. If the starting value is already past limit, the timer doesn't go off.

After the repetition whose count is limit, the timer finishes: it stops, its period reads 0, and the count goes back to its starting value. Giving it a period again repeats the same run.

Setting period to 0 before the timer finishes pauses it; giving it a period again carries on the count from where it was.

Assigning a value to the primary field (<t n>) sets the count of the next repetition, and also becomes the starting value the count goes back to when the timer finishes. If the value is within limit, the timer carries on from there; if it's past limit, the timer finishes straight away.

The timing is approximate: each repetition takes a little time to carry out, so a timer will drift slowly behind a real clock.

Notes

A convenient way to control a timer is to create it with period=0 limit=inf, then start and stop it by changing period. Stopping and restarting this way carries on the count; to start the count again, assign it, e.g. <t 1>.

Example

<tenths:scalar 0>
<ticker:timer period=0 limit=inf onTimer=<tenths | add tenths, 1>, <shown | div tenths, 10>>
<shown:span {0}>
<button {Start} onClick=<ticker.period 100>> <button {Stop} onClick=<ticker.period 0>>

See also