Tickers in COBOL
Timers are for when you want to do something once in the future - tickers are for when you want to do something repeatedly at regular intervals. Here’s an example of a ticker that ticks periodically until we stop it.
In COBOL, we don’t have built-in tickers or timers like in more modern languages. Instead, we simulate a ticker by continuously checking the current time and performing an action at regular intervals.
We use the ACCEPT ... FROM TIME
statement to get the current time in hundredths of a second since midnight. We calculate the elapsed time since the start of the program and use the MOD
function to determine if it’s time for a “tick”.
The program will run until the elapsed time reaches 1600 hundredths of a second (16 seconds), simulating the behavior of stopping the ticker after a certain duration.
When we run this program, it should display several “ticks” before stopping:
Note that the exact times will vary depending on when you run the program.
This COBOL implementation doesn’t use channels or goroutines as in the original example, as COBOL doesn’t have these concepts. Instead, it uses a simple loop with time-based checks to simulate periodic ticking.