In this guide, we'll walk through the process of building a ToolJet application that automates data retrieval at specific intervals. By utilizing the RunJS queries, we can set up intervals for triggering queries, ensuring that the data is fetched dynamically and efficiently.
Begin by creating a new application in the ToolJet dashboard. Once the app builder opens, Drag a table component onto the canvas. This component will display the data fetched from the REST API query.
<divstyle={{textAlign:'center'}}>
<imgstyle={{border:'0',marginBottom:'15px',borderRadius:'5px',boxShadow:'0px1px3pxrgba(0,0,0,0.2)'}}className="screenshot-full"src="/img/how-to/setinterval/table.png"alt="Table Component With Data"/>
From the query panel, create a new REST API query. Utilize mock REST API data by choosing the 'GET' method and specifying the endpoint (e.g., `https://jsonplaceholder.typicode.com/posts`). Name the query 'post' and `Run` the query to ensure that the data is fetched successfully.
<divstyle={{textAlign:'center'}}>
<imgstyle={{border:'0',marginBottom:'15px',borderRadius:'5px',boxShadow:'0px1px3pxrgba(0,0,0,0.2)'}}className="screenshot-full"src="/img/how-to/setinterval/queryprev.png"alt="Table Component With Data"/>
In the Table properties, link the query data to the table by setting the 'table data' property to `{{queries.post.data}}`. This establishes the connection between the REST API query and the table component.
<divstyle={{textAlign:'center'}}>
<imgstyle={{border:'0',marginBottom:'15px',borderRadius:'5px',boxShadow:'0px1px3pxrgba(0,0,0,0.2)'}}className="screenshot-full"src="/img/how-to/setinterval/queryp.png"alt="Table Component With Data"/>
From the Settings section of the RunJS query, enable 'Run query on page load.' This ensures that the query is triggered when the application is loaded. Rename the query as 'setInterval' to complete the configuration.
<divstyle={{textAlign:'center'}}>
<imgstyle={{border:'0',marginBottom:'15px',borderRadius:'5px',boxShadow:'0px1px3pxrgba(0,0,0,0.2)'}}className="screenshot-full"src="/img/how-to/setinterval/settings.png"alt="Table Component With Data"/>
Create another RunJS query named 'clearInrternal' to stop the query from triggering indefinitely. Use the `clearInterval()` method to clear the interval. This method retrieves the value from the variable set in the 'setInterval' query.
Drag a button on the canvas to act as a user-triggered stop mechanism. Attach an event handler to execute the 'clear' query when the button is clicked.
<divstyle={{textAlign:'center'}}>
<imgstyle={{border:'0',marginBottom:'15px',borderRadius:'5px',boxShadow:'0px1px3pxrgba(0,0,0,0.2)'}}className="screenshot-full"src="/img/how-to/setinterval/clearint.png"alt="Table Component With Data"/>
By following these steps, your ToolJet application will dynamically fetch data at specified intervals, providing an efficient and automated user experience.