TIMER
This element is not an actual data type but simply fires an event on a regular basis, when specified time has elapsed.
Attributes brief detailed show all inherited
More information
Timer can help you do various task: change parameter over time, check for changes that can't be tracked other way, create animation, create delayed action, etc.
Timer hints
Timer parameter refresh_time_ms usually cannot be smaller than 10ms.
Timer speeds on Windows and Mac can be different.
Timer is affected by SKIN parameter refresh_priority (when it is set to "idle" timers runs slower).
ACTION_TRIGGER with async="true" for a timer tends to crash, use async="false".
Continuous action
- <!-- a timer and an action -->
- <TIMER id="forever_timer" refresh_time_ms="10" />
- <ACTION_TRIGGER event_id="forever_timer.elapsed"
- script="
- demo_param = 0.1+demo_param;
- wiggle.h_offset = sin(demo_param)*100;
- " requires="demo_param;wiggle.h_offset" />
- <!-- simple param and widget to move -->
- <PARAM id="demo_param" default="0" max="$_PARAM_MAX_$"/>
- <ROW width="200">
- <WIDGET id="wiggle" background_color="#000080" width="10" height="10" />
- </ROW>
Action while condition
Timer keeps running but the action is not triggered because condition_formula is false.
- <!-- a timer and an action -->
- <TIMER id="regular_timer" refresh_time_ms="50" />
- <ACTION_TRIGGER event_id="regular_timer.elapsed"
- script="demo_param = 1+demo_param;" requires="demo_param"
- condition_formula="(demo_param < 30)" />
- <!-- demo param -->
- <PARAM id="demo_param" default="0" max="$_PARAM_MAX_$"/>
- <PARAM_TEXT_CONTROL param_id="demo_param" />
Run and stop
You can enable and disable timer on the fly. The following code creates a timer launched by a button that calls a simple script (using an ACTION_TRIGGER) measuring elapsed time while it is displayed on screen. Once elapsed_time reaches 1000 ms, timer is stopped and a message box is displayed.
- <!-- Creates a disabled demo_timer -->
- <TIMER id="demo_timer" enabled="false" refresh_time_ms="10" />
- <!-- Parameters used by the script-->
- <PARAM id="times_clicked" default="0" max="$_PARAM_MAX_$" exposed="true"/>
- <PARAM id="times_finished" default="0" max="$_PARAM_MAX_$" exposed="true"/>
- <PARAM id="elapsed" default="0" max="2000" exposed="true"/>
- <!-- Script action trigger on demo_timer event -->
- <ACTION_TRIGGER event_id="demo_timer.elapsed" script="
- elapsed += demo_timer.refresh_time_ms;
- if (elapsed >= 1000) {
- demo_timer.enabled = 0;
- times_finished = 1+times_finished;
- elapsed = 0;
- }" async="false" requires="demo_timer.refresh_time_ms;demo_timer.enabled" />
- <!-- Simple "click me" button to start the demo_timer -->
- <COLUMN margin="5">
- <WIDGET background_color="#333333" v_margin="10" h_margin="10">
- <TEXT value="Click Here" text_color="#EEEEEE" >
- <INVISIBLE_PARAM_BUTTON id="button" width="100%" height="100%" param_id="demo_timer.enabled"/>
- <ACTION_TRIGGER event_id="demo_timer.enabled.value_changed" condition_formula="demo_timer.enabled==1" script="times_clicked = 1+times_clicked;" requires="times_clicked" />
- </TEXT>
- </WIDGET>
- <CELL height="10" />
- <!-- displaying text data -->
- <PARAM_TEXT font_size="15" param_id="elapsed" content="button clicked {value} ms ago" width="300" value_format="0." />
- <ROW>
- <PARAM_TEXT param_id="times_clicked" content="times clicked: {value} " value_format="0."/>
- <PARAM_TEXT param_id="times_finished" content="times finished: {value}" value_format="0."/>
- </ROW>
- </COLUMN>