Your program can repeat things with its own loops, of course. But sometimes it's handy for the console to repeat something itself: generating a list of values, doing the same thing to every element of an array, or doing something again and again over time. ioL has two tags for this: for, which repeats as fast as it can, and timer, which repeats at regular intervals.
A for tag counts from one number to another, and carries out the code in its do field once for each number:
<n:for 1 limit=5 do=n> <! generates 1, 2, 3, 4, 5 !>
Whatever do generates each time round, the for tag generates too, one after another. So this displays the seven times table:
<n:for 1 limit=12 do={<n> x 7 = <mul n, 7></n>}>
1 x 7 = 7And this adds up the numbers from 1 to 100, by carrying out an action each time round instead:
<total:scalar 0> <m:for 1 limit=100 do=<total | add total, m>> <putLn total> <! sends 5050 !>
A for loop generates values, and can carry out actions, but it can't create presentation tags: a span in its do field won't appear on screen. To create many tag instances from one definition, use an array, which is what arrays are for.
A for loop is the natural way to do something to every element of an array: make each one the current element in turn, and act on it. Here's how you could clear the noughts and crosses board from the chapter on arrays, to start a new game:
<i:for 1 limit=cells.# do=<cells i>, <cells.*.mark {</ >}>>
While a for loop is running, nothing else can happen in the console: the user can't click anything until it's finished. To keep the console responsive, a loop is cut off after 4096 times round (this may vary between computers), without any warning. So use for for quick jobs on a handful of things. Anything bigger belongs in your program.
A timer carries out the code in its onTimer field every so often. Unlike for, it waits between repetitions, and the user can carry on using the console in the meantime.
<t:timer period=1,{s} limit=10 onTimer=<putLn {tick}>>
This sends tick to your program once a second, ten times.
1,{s}, or
milliseconds (thousandths of a second), as 200,{ms}. A number on its own means milliseconds.Does that remind you of anything? A timer is really a for loop that takes its time. It has a
count in its primary field, which starts at 1 and goes up by one each time the timer goes off. While the
onTimer code runs, the count is the number of the current tick, just as a for
loop's name stands for the current count in do. And, just as in a for loop,
limit is the last value of the count. That's why limit=10 above means ten ticks.
You can give the count a different starting value too. This one goes off eleven times, counting from 0 to 10:
<t:timer 0 period=1,{s} limit=10 onTimer=<putLn t>>
Unlike a for tag, a timer is a persistent logic tag: it stays in the console until you clear it, and you can control it by changing its fields.
A timer with a period of 0 is stopped. So the easy way to control a timer is to create it with
period=0 and limit=inf, then start it by giving it a period, and stop it again by
setting the period back to 0:
<t:timer period=0 limit=inf onTimer=...>
<t.period 1,{s}> <! start: go off every second !>
<t.period 0> <! stop !>
Starting it again carries on where it left off.
What happens when a timer reaches its limit? It finishes: it stops by itself, its period goes back to 0, and its count goes back to where it started. So giving it a period again doesn't carry on; it repeats the same run from the beginning.
You can also set the count yourself, at any time. <t 1> makes the next tick number 1, so the
count starts again, and a running timer keeps going. Set the count past limit, and the timer
finishes straight away.
This stopwatch runs entirely in the console. The timer goes off every tenth of a second, and each time it adds one to tenths and displays the time in seconds.
print('''
<console.title {Stopwatch}>
<tenths:scalar 0>
<ticker:timer period=0 limit=inf onTimer=<tenths | add tenths, 1>, <shown | div tenths, 10>>
<shown:span size=48,pt bold=true {0}></n>
<button {Start} onClick=<ticker.period 100>> <button {Stop} onClick=<ticker.period 0>>
<button {Reset} onClick=<tenths 0>, <shown 0>> <button {Quit} onClick=<putLn {/quit}>>
''')
while input() != '/quit':
pass
3.1The stopwatch keeps its own count in tenths, rather than using the timer's count, so that Reset can set it back to zero without touching the timer.
A timer goes off about as often as you ask. It's good enough for animations and for keeping a display up to date, but this stopwatch will drift slowly behind a real clock. If you need accurate timing, have your program measure the time, and use the timer only to ask the program for an update.