Timers
Timers let you create and manage periodic client-side tasks in an application. They automatically execute JavaScript code at specified intervals to perform operations, such as updating data or sending notifications. This document describes how to configure and use timers in the Mobi platform.
Timer scope
Page-level timers
Page-level timers are active only on the specific page where they are created. They can reference other values on the page. When a user navigates away from the current page, the page-level timer stops automatically.
Global timers
Once started, a global timer runs for the entire lifecycle of the application. The timer does not stop automatically, even if the user switches pages.
Configure a timer
In the Mobi platform, configuring a timer involves the following key settings:
Parameters: The input values required for the timer's execution. Parameters can be static values or dynamic expressions.
Execution script: Contains the code block that runs at each interval. This script contains the core logic of the timer, which is written in JavaScript.
Time interval: The frequency at which the timer triggers. This value is specified in milliseconds and defines how often the timer executes.
Description: A brief explanation of the timer's function and purpose. This helps other developers understand what the timer does.
Execution prevention condition: A conditional expression that prevents the timer from starting. The timer starts only if this condition is not met. This feature prevents the timer from running in unintended scenarios.
Invocation methods
Timers support two basic invocation methods:
Start: Activates the timer to begin executing at the set interval. If the timer is already running, this method restarts it.
Stop: Deactivates the timer and cancels all subsequent scheduled executions.
In an event handler, select the "Manage Timer" action. Alternatively, in an execution script, use [timerId].start() or [timerId].stop() to invoke the timer.
Notes
The timer's execution prevention condition is checked only when the timer starts. To conditionally execute the script at each interval, call a frontend function from within the execution script and set the execution prevention condition for that function.
To stop the timer based on a condition during an execution cycle, call its stop method from within the execution script.
Parameters passed to a timer are static and do not change. However, references within the execution script to items such as components and variables always reflect their current values.