Timers
Timers allow you to call any type of trigger, including APIs, repeatedly at a timed interval.
Auto-refresh a dashboard with a Timer
We can refresh any component's data automatically with a Timer. For this example we'll show you how to do it for a chart component, but it works equally as well with tables, maps, and any other Component.
Start by dragging the Chart component from the Components panel to the empty canvas. Set the Chart Header to Number of Signups
, the X axis title to Day of month
and the Y axis title to User Signups
.
To populate the chart from an external source, we can use the Superblocks API builder. In this case, we will use a JavaScript step that generates sample user signup data for us.
- Create a new backend API with a JavaScript step
- Rename API1 to
getSignups
- Copy and paste the following code to the JavaScript step
data = [];
for(var i=1; i<10; i++){
const date = i;
const value = Math.floor(Math.random() * 60) + 1;
point = {};
point.date = date;
point.value = value;
data = [point, ...data];
}
return data;
In the application User Interface, click on the chart to open the Properties panel. To connect the results from getSignups
API to the chart, replace the example JSON with {{getSignups.response}}
in the Chart Data field and Run the API. Finally set Chart Type to Line Chart
.
Turn off the Loading Animation setting for a smooth animation.
Next, link the Chart Component to a Timer that updates it at an intervals.
- Click on Timers (clock icon) in the left sidebar
- Click on + Add timer
- Select Timer1
- Set Interval to
5000
(5 seconds) - Click on Triggers
- Set the Action Type to
Run APIs
- Set the API to
getSignups
That's it, your new User Signup Dashboard will now automatically update with the latest signups every 5 seconds.
Starting and stopping timers
Starting timers on page load
You can control whether a Timer starts on page load by the checking the box in the Timers edit interface.
Starting and stopping from component triggers
You can hook up any Component trigger (like a Button component's onClick
) to start and stop Timers. Just choose the Start/stop Timer option from the triggers menu.
You can also start or stop timers from code inside of a Run JS action. See Programmatic usage (using code to control Timers)
Starting and stopping timers from the UI
You can start or stop a timer from the UI by clicking the start/stop button in the timers edit interface.
Programmatic usage (using code to control Timers)
You can start or stop a Timer using code in Run JS (in browser) actions by referencing its name and calling start
or stop
.
Timer1.start();
Timer1.stop();
You can also "toggle" a timer without needing to know if it's started or stopped by calling toggle
.
Timer1.toggle();
Properties
Timers properties can be accessed from other frontend components and backend APIs by adding the name of the Timer, and dot referencing the property. For a Timer named Timer1:
Property | Description |
---|---|
Timer1.isActive | Access whether the Timer is currently running |
Troubleshooting
How do I not show the loading animation when my API is running?
Turn off the Loading Animation setting in the Properties Panel of your Charts Component to update your Charts without showing a loading animation.