Carries out code repeatedly, with a time delay between each repetition.
<t:timer period=1,{s} limit=10 onTimer=<putLn {tick}>> sends tick to the program once a second, ten times.<t:timer period=0 limit=inf onTimer=...> creates a stopped timer, which can be started later.<t.period 500> starts the timer, or changes its speed, to every half a second.<t.period 0> stops the timer.<t:timer 0 period=1,{s} limit=10 onTimer=...> counts from 0 to 10, once a second (eleven times).<t 1> makes the next count 1, starting the count again.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.
1,{s} for seconds, 200,{ms} for milliseconds; a number on its own is milliseconds. A period of 0 stops the timer.inf to repeat indefinitely.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.
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>.
<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>>