AutoCall Timers

AutoCall schedules a function to run automatically on a timer, measured in milliseconds – no threads, no manual frame-counting. It's the tidy answer to "do this every so often": a clock that updates once a second, an enemy wave every ten seconds, a one-off message after a short delay.

AutoCall UpdateClock Every 1000     ' call UpdateClock() every 1000ms (repeats)
AutoCall SpawnWave   Once  10000    ' call SpawnWave() once, 10000ms from now
StopCall UpdateClock                ' cancel a repeating timer
StopCall All                        ' cancel every timer


How it works

The handler is a plain Function with no arguments, referred to by name (no parentheses). Every repeats until you stop it; Once fires a single time and removes itself.

Global ticks:Int = 0

Function OnTick()
    ticks = ticks + 1
    Print "Tick {%ticks}"
EndFunction

Function Main()
    AutoCall OnTick Every 500       ' twice a second
    ; ... keep the program running (see below) ...
    Return False
EndFunction


The rules (they keep it simple)

Keeping the program running (Desktop vs Web)

A timer only fires while the program is alive to check it. That plays out differently on the two targets:

Desktop – loop until done:

Function Main()
    AutoCall OnTick Every 500
    Repeat
    Until ticks >= 6          ' the loop keeps the scheduler ticking
    StopCall OnTick
    Return False
EndFunction

Web – set timers and return (the handler stops itself):

Function OnTick()
    ticks = ticks + 1
    Print "Tick {%ticks}"
    If ticks >= 6
        StopCall OnTick
    EndIf
EndFunction

Function Main()
    AutoCall OnTick Every 500
    Return False             ' the browser keeps the timers firing
EndFunction


Why not just count frames?

The hand-rolled alternative – If frameCount Mod 50 = 0 scattered through the main loop – breaks the moment the frame rate changes. AutoCall ... Every 1000 is tied to real elapsed time, so it stays correct whether the program runs fast or slow.



Key Points

See Also

Examples

See AutoCall.bam. (The Windows documentation also points at an AutoCall_web.bam; the web target is not part of this edition, so that example is not shipped here.)


BambooBasic © 2026 Michael Denathorn