Timer start

From Sim Innovations Wiki
Jump to navigation Jump to search


timer_id = timer_start(delay) (from AM/AP 4.1)
timer_id = timer_start(delay, callback) (from AM/AP 3.5)
timer_id = timer_start(delay, period, callback)
timer_id = timer_start(delay, period, nr_calls, callback) (from AM/AP 3.5)

timer_start is used to start a timer, which will call a function at an configurable interval.

Return value

Argument Type Description
timer_id ID This value can be used for further reference. Functions such as timer_stop can use this timer_id. Its good practice to store this image_id in your logic code.


# Argument Type Description
1 delay Number Initial delay in milliseconds. After calling timer_start, the callback function will be executed after this delay.
2 period Number (Optional) Time between two function calls, in milliseconds. Providing it with 'nil' will make the function only get called once.
3 nr_calls Number (Optional) Number of callbacks.
4 callback Number (Optional, from AM/AP 4.1) This function will be called at every interval. Two arguments will be provided, the count and max (can be -1 if running infinitely).

Example (one shot)

-- This function will be called once after one second
function timer_callback()
    print("Timer callback")

timer_start(1000, timer_callback)

Example (interval)

-- This function will be called every second
function timer_callback(count)
    print("Timer callback " .. count)

timer_start(0, 1000, timer_callback)

Example (limited call count)

-- This function will be called 5 times, once every second
function timer_callback(count, max)
    print("Timer callback " .. count .. " of " .. max)

timer_start(0, 1000, 5, timer_callback)