timer = Timer ( ) timer:start ( 1000, true ) timer.on_tick = function ( timer ) -- will be called once every 1000 ms local ms = timer:get_delta_ms ( ) end
It is never guaranteed that your handler function will be called precisely at the desired time interval. For example, let's say that we have created a recurring timer with an interval of 16 milliseconds. However, the callback function takes 32 milliseconds to execute. In such a case, the callback function would be evoked half less frequently than we expect. To accommodate for the potential delay between callbacks, we have to look at the timer's delta value. The delta value shows the actual amount of time elapsed since the last callback. Your program must take delta into account at each callback in order to produce smooth, frame-independent effects.
display:create ( "Clock example", 800, 600, 32, true ) arrow = Sprite ( ) arrow.canvas:line_to ( 0, 200 ) arrow.canvas:set_line_style ( 5, WHITE, 1 ) arrow.canvas:stroke ( ) display.viewport:add_child ( arrow ) timer = Timer ( ) timer:start ( 16, true ) timer.on_tick = function ( timer ) local delta = timer:get_delta_ms ( ) arrow.rotation = arrow.rotation + 0.006 * delta end