Run query at specified intervals
In this how-to guide, we will learn how to make a query trigger at the specific intervals.
Let's go to the ToolJet dashboard and create a new application
Once the app builder opens up, drag a table component to canvas
Now, create a new REST API query from the query panel at the bottom of the app builder. We will be using the data from the mock REST API and then load the data on the table. Let's create a REST API, choose
GETmethod from the dropdown, enter the endpoint(https://jsonplaceholder.typicode.com/posts), name the querypostand then save and run it
Go to the Table properties and add connect the query data to table by adding value to table data property which is
{{queries.post.data}}
Now, we will create a RunJS query that will first set a variable called
intervalwhich will include the value returned by thesetInterval()method that calls a functioncountdownat specified intervals. The countdown function has the code to trigger thepostquery that we created in the previous step.actions.setVariable('interval',setInterval(countdown, 5000));
function countdown(){
queries.post.run()
}Go to the Advanced tab of the query, enable
Run query on page load?this will trigger this RunJS query when the app is loaded. Name the query assetand Save it. Note that you will have to save the query and notSave and Runbecause doing it will trigger the query and you won't be able to stop the query unless you reload the page or go back to dashboard.
To prevent the query from triggering indefinitely, we will create another RunJS query that will make use of
clearInterval()method. In this method we will get the value from the variable that we created insetquery. Save this query asclear.clearInterval(variables.interval)Finally, let's add a button on to the canvas and add the event handler to the button to run the
clearquery.Now, whenever the app will be loaded the set query will be triggered and will keep triggering the
postquery at the specified intervals. Whenever the user wants to stop the query they can click on the button to trigger the clear query which will clear the interval.